blob: 273f1c547f29fb8dd92e027fac5b4e7c780c1168 [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)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700121pub enum Constness {
122 Const,
123 NotConst,
124}
125
126#[derive(Debug, Copy, Clone, Eq, PartialEq)]
127pub enum Defaultness {
128 Default,
129 Final,
130}
131
David Tolnayf38cdf62016-09-23 19:07:09 -0700132/// Foreign module declaration.
133///
David Tolnay35902302016-10-06 01:11:08 -0700134/// E.g. `extern { .. }` or `extern "C" { .. }`
David Tolnayf38cdf62016-09-23 19:07:09 -0700135#[derive(Debug, Clone, Eq, PartialEq)]
136pub struct ForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700137 pub abi: Abi,
David Tolnayf38cdf62016-09-23 19:07:09 -0700138 pub items: Vec<ForeignItem>,
139}
140
141#[derive(Debug, Clone, Eq, PartialEq)]
142pub struct ForeignItem {
David Tolnayb79ee962016-09-04 09:39:20 -0700143 pub ident: Ident,
144 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700145 pub node: ForeignItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -0700146 pub vis: Visibility,
David Tolnayf38cdf62016-09-23 19:07:09 -0700147}
148
David Tolnay771ecf42016-09-23 19:26:37 -0700149/// An item within an `extern` block
David Tolnayf38cdf62016-09-23 19:07:09 -0700150#[derive(Debug, Clone, Eq, PartialEq)]
151pub enum ForeignItemKind {
152 /// A foreign function
153 Fn(Box<FnDecl>, Generics),
David Tolnay35902302016-10-06 01:11:08 -0700154 /// A foreign static item (`static ext: u8`)
155 Static(Box<Ty>, Mutability),
David Tolnayf38cdf62016-09-23 19:07:09 -0700156}
157
158/// Represents an item declaration within a trait declaration,
159/// possibly including a default implementation. A trait item is
160/// either required (meaning it doesn't have an implementation, just a
161/// signature) or provided (meaning it has a default implementation).
162#[derive(Debug, Clone, Eq, PartialEq)]
163pub struct TraitItem {
164 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700165 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700166 pub node: TraitItemKind,
167}
168
169#[derive(Debug, Clone, Eq, PartialEq)]
170pub enum TraitItemKind {
171 Const(Ty, Option<Expr>),
172 Method(MethodSig, Option<Block>),
173 Type(Vec<TyParamBound>, Option<Ty>),
174 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700175}
176
David Tolnay55337722016-09-11 12:58:56 -0700177#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700178pub enum ImplPolarity {
179 /// `impl Trait for Type`
180 Positive,
181 /// `impl !Trait for Type`
182 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700183}
184
David Tolnayf38cdf62016-09-23 19:07:09 -0700185#[derive(Debug, Clone, Eq, PartialEq)]
186pub struct ImplItem {
187 pub ident: Ident,
188 pub vis: Visibility,
189 pub defaultness: Defaultness,
190 pub attrs: Vec<Attribute>,
191 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700192}
193
David Tolnayf38cdf62016-09-23 19:07:09 -0700194#[derive(Debug, Clone, Eq, PartialEq)]
195pub enum ImplItemKind {
196 Const(Ty, Expr),
197 Method(MethodSig, Block),
198 Type(Ty),
199 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700200}
David Tolnayd5025812016-09-04 14:21:46 -0700201
David Tolnayf38cdf62016-09-23 19:07:09 -0700202/// Represents a method's signature in a trait declaration,
203/// or in an implementation.
204#[derive(Debug, Clone, Eq, PartialEq)]
205pub struct MethodSig {
206 pub unsafety: Unsafety,
207 pub constness: Constness,
David Tolnay0aecb732016-10-03 23:03:50 -0700208 pub abi: Option<Abi>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700209 pub decl: FnDecl,
210 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700211}
David Tolnayedf2b992016-09-23 20:43:45 -0700212
David Tolnay62f374c2016-10-02 13:37:00 -0700213/// Header (not the body) of a function declaration.
214///
215/// E.g. `fn foo(bar: baz)`
216#[derive(Debug, Clone, Eq, PartialEq)]
217pub struct FnDecl {
218 pub inputs: Vec<FnArg>,
219 pub output: FunctionRetTy,
David Tolnay292e6002016-10-29 22:03:51 -0700220 pub variadic: bool,
David Tolnay62f374c2016-10-02 13:37:00 -0700221}
222
223/// An argument in a function header.
224///
225/// E.g. `bar: usize` as in `fn foo(bar: usize)`
226#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayca085422016-10-04 00:12:38 -0700227pub enum FnArg {
228 SelfRef(Option<Lifetime>, Mutability),
229 SelfValue(Mutability),
230 Captured(Pat, Ty),
231 Ignored(Ty),
David Tolnay62f374c2016-10-02 13:37:00 -0700232}
233
David Tolnayedf2b992016-09-23 20:43:45 -0700234#[cfg(feature = "parsing")]
235pub mod parsing {
236 use super::*;
David Tolnay4a057422016-10-08 00:02:31 -0700237 use {DelimToken, FunctionRetTy, Generics, Ident, Mac, Path, TokenTree, VariantData, Visibility};
David Tolnay7b8009b2016-10-25 22:36:00 -0700238 use attr::parsing::{inner_attr, outer_attr};
David Tolnay2f9fa632016-10-03 22:08:48 -0700239 use data::parsing::{struct_like_body, visibility};
David Tolnay62f374c2016-10-02 13:37:00 -0700240 use expr::parsing::{block, expr, pat};
David Tolnayca085422016-10-04 00:12:38 -0700241 use generics::parsing::{generics, lifetime, ty_param_bound, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700242 use ident::parsing::ident;
David Tolnay84aa0752016-10-02 23:01:13 -0700243 use mac::parsing::delimited;
David Tolnayedf2b992016-09-23 20:43:45 -0700244 use macro_input::{Body, MacroInput};
245 use macro_input::parsing::macro_input;
David Tolnayb8d8ef52016-10-29 14:30:08 -0700246 use ty::parsing::{abi, mutability, path, ty, unsafety};
David Tolnayedf2b992016-09-23 20:43:45 -0700247
248 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700249 item_extern_crate
David Tolnay4a057422016-10-08 00:02:31 -0700250 |
251 item_use
David Tolnay47a877c2016-10-01 16:50:55 -0700252 |
253 item_static
254 |
255 item_const
David Tolnay42602292016-10-01 22:25:45 -0700256 |
257 item_fn
David Tolnay35902302016-10-06 01:11:08 -0700258 |
259 item_mod
260 |
261 item_foreign_mod
David Tolnay3cf52982016-10-01 17:11:37 -0700262 |
263 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700264 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700265 item_struct_or_enum
David Tolnay2f9fa632016-10-03 22:08:48 -0700266 |
267 item_union
David Tolnay0aecb732016-10-03 23:03:50 -0700268 |
269 item_trait
David Tolnayf94e2362016-10-04 00:29:51 -0700270 |
271 item_default_impl
David Tolnay4c9be372016-10-06 00:47:37 -0700272 |
273 item_impl
David Tolnay84aa0752016-10-02 23:01:13 -0700274 |
275 item_mac
276 ));
277
David Tolnay453cfd12016-10-23 11:00:14 -0700278 named!(pub items -> Vec<Item>, many0!(item));
279
David Tolnay84aa0752016-10-02 23:01:13 -0700280 named!(item_mac -> Item, do_parse!(
281 attrs: many0!(outer_attr) >>
282 path: ident >>
283 punct!("!") >>
284 name: option!(ident) >>
285 body: delimited >>
David Tolnay1a8b3522016-10-08 22:27:00 -0700286 cond!(match body.delim {
287 DelimToken::Paren | DelimToken::Bracket => true,
288 DelimToken::Brace => false,
289 }, punct!(";")) >>
David Tolnay84aa0752016-10-02 23:01:13 -0700290 (Item {
291 ident: name.unwrap_or_else(|| Ident::new("")),
292 vis: Visibility::Inherited,
293 attrs: attrs,
294 node: ItemKind::Mac(Mac {
295 path: path.into(),
296 tts: vec![TokenTree::Delimited(body)],
297 }),
298 })
David Tolnayedf2b992016-09-23 20:43:45 -0700299 ));
300
David Tolnaya96a3fa2016-09-24 07:17:42 -0700301 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700302 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700303 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700304 keyword!("extern") >>
305 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700306 id: ident >>
307 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700308 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700309 ident
310 )) >>
311 punct!(";") >>
312 ({
313 let (name, original_name) = match rename {
314 Some(rename) => (rename, Some(id)),
315 None => (id, None),
316 };
317 Item {
318 ident: name,
319 vis: vis,
320 attrs: attrs,
321 node: ItemKind::ExternCrate(original_name),
322 }
323 })
324 ));
325
David Tolnay4a057422016-10-08 00:02:31 -0700326 named!(item_use -> Item, do_parse!(
327 attrs: many0!(outer_attr) >>
328 vis: visibility >>
329 keyword!("use") >>
330 what: view_path >>
331 punct!(";") >>
332 (Item {
333 ident: "".into(),
334 vis: vis,
335 attrs: attrs,
336 node: ItemKind::Use(Box::new(what)),
337 })
338 ));
339
340 named!(view_path -> ViewPath, alt!(
341 view_path_glob
342 |
343 view_path_list
344 |
345 view_path_list_root
346 |
347 view_path_simple // must be last
348 ));
349
350
351 named!(view_path_simple -> ViewPath, do_parse!(
352 path: path >>
353 rename: option!(preceded!(keyword!("as"), ident)) >>
354 (ViewPath::Simple(path, rename))
355 ));
356
357 named!(view_path_glob -> ViewPath, do_parse!(
358 path: path >>
359 punct!("::") >>
360 punct!("*") >>
361 (ViewPath::Glob(path))
362 ));
363
364 named!(view_path_list -> ViewPath, do_parse!(
365 path: path >>
366 punct!("::") >>
367 punct!("{") >>
368 items: separated_nonempty_list!(punct!(","), path_list_item) >>
369 punct!("}") >>
370 (ViewPath::List(path, items))
371 ));
372
373 named!(view_path_list_root -> ViewPath, do_parse!(
374 global: option!(punct!("::")) >>
375 punct!("{") >>
376 items: separated_nonempty_list!(punct!(","), path_list_item) >>
377 punct!("}") >>
378 (ViewPath::List(Path {
379 global: global.is_some(),
380 segments: Vec::new(),
381 }, items))
382 ));
383
384 named!(path_list_item -> PathListItem, do_parse!(
David Tolnaye6e42542016-10-24 22:37:11 -0700385 name: alt!(
386 ident
387 |
388 map!(keyword!("self"), Into::into)
389 ) >>
David Tolnay4a057422016-10-08 00:02:31 -0700390 rename: option!(preceded!(keyword!("as"), ident)) >>
391 (PathListItem {
392 name: name,
393 rename: rename,
394 })
395 ));
396
David Tolnay47a877c2016-10-01 16:50:55 -0700397 named!(item_static -> Item, do_parse!(
398 attrs: many0!(outer_attr) >>
399 vis: visibility >>
400 keyword!("static") >>
401 mutability: mutability >>
402 id: ident >>
403 punct!(":") >>
404 ty: ty >>
405 punct!("=") >>
406 value: expr >>
407 punct!(";") >>
408 (Item {
409 ident: id,
410 vis: vis,
411 attrs: attrs,
412 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
413 })
414 ));
415
416 named!(item_const -> Item, do_parse!(
417 attrs: many0!(outer_attr) >>
418 vis: visibility >>
419 keyword!("const") >>
420 id: ident >>
421 punct!(":") >>
422 ty: ty >>
423 punct!("=") >>
424 value: expr >>
425 punct!(";") >>
426 (Item {
427 ident: id,
428 vis: vis,
429 attrs: attrs,
430 node: ItemKind::Const(Box::new(ty), Box::new(value)),
431 })
432 ));
433
David Tolnay42602292016-10-01 22:25:45 -0700434 named!(item_fn -> Item, do_parse!(
435 attrs: many0!(outer_attr) >>
436 vis: visibility >>
437 constness: constness >>
438 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700439 abi: option!(abi) >>
David Tolnay42602292016-10-01 22:25:45 -0700440 keyword!("fn") >>
441 name: ident >>
442 generics: generics >>
443 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700444 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay42602292016-10-01 22:25:45 -0700445 punct!(")") >>
446 ret: option!(preceded!(punct!("->"), ty)) >>
447 where_clause: where_clause >>
448 body: block >>
449 (Item {
450 ident: name,
451 vis: vis,
452 attrs: attrs,
453 node: ItemKind::Fn(
454 Box::new(FnDecl {
455 inputs: inputs,
456 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700457 variadic: false,
David Tolnay42602292016-10-01 22:25:45 -0700458 }),
459 unsafety,
460 constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700461 abi,
David Tolnay42602292016-10-01 22:25:45 -0700462 Generics {
463 where_clause: where_clause,
464 .. generics
465 },
466 Box::new(body),
467 ),
468 })
469 ));
470
David Tolnayca085422016-10-04 00:12:38 -0700471 named!(fn_arg -> FnArg, alt!(
472 do_parse!(
473 punct!("&") >>
474 lt: option!(lifetime) >>
475 mutability: mutability >>
476 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700477 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700478 (FnArg::SelfRef(lt, mutability))
479 )
480 |
481 do_parse!(
482 mutability: mutability >>
483 keyword!("self") >>
David Tolnay7a2b6ed2016-10-25 22:15:38 -0700484 not!(peek!(punct!(":"))) >>
David Tolnayca085422016-10-04 00:12:38 -0700485 (FnArg::SelfValue(mutability))
486 )
487 |
488 do_parse!(
489 pat: pat >>
490 punct!(":") >>
491 ty: ty >>
492 (FnArg::Captured(pat, ty))
493 )
494 |
495 ty => { FnArg::Ignored }
David Tolnay62f374c2016-10-02 13:37:00 -0700496 ));
497
David Tolnay35902302016-10-06 01:11:08 -0700498 named!(item_mod -> Item, do_parse!(
David Tolnay7b8009b2016-10-25 22:36:00 -0700499 outer_attrs: many0!(outer_attr) >>
David Tolnay35902302016-10-06 01:11:08 -0700500 vis: visibility >>
501 keyword!("mod") >>
502 id: ident >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700503 content: alt!(
David Tolnay37d10332016-10-13 20:51:04 -0700504 punct!(";") => { |_| None }
505 |
David Tolnay7b8009b2016-10-25 22:36:00 -0700506 delimited!(
507 punct!("{"),
508 tuple!(
509 many0!(inner_attr),
510 items
511 ),
512 punct!("}")
513 ) => { Some }
David Tolnay37d10332016-10-13 20:51:04 -0700514 ) >>
David Tolnay7b8009b2016-10-25 22:36:00 -0700515 (match content {
516 Some((inner_attrs, items)) => Item {
517 ident: id,
518 vis: vis,
519 attrs: {
520 let mut attrs = outer_attrs;
521 attrs.extend(inner_attrs);
522 attrs
523 },
524 node: ItemKind::Mod(Some(items)),
525 },
526 None => Item {
527 ident: id,
528 vis: vis,
529 attrs: outer_attrs,
530 node: ItemKind::Mod(None),
531 },
David Tolnay35902302016-10-06 01:11:08 -0700532 })
533 ));
534
535 named!(item_foreign_mod -> Item, do_parse!(
536 attrs: many0!(outer_attr) >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700537 abi: abi >>
David Tolnay35902302016-10-06 01:11:08 -0700538 punct!("{") >>
539 items: many0!(foreign_item) >>
540 punct!("}") >>
541 (Item {
542 ident: "".into(),
543 vis: Visibility::Inherited,
544 attrs: attrs,
545 node: ItemKind::ForeignMod(ForeignMod {
David Tolnayb8d8ef52016-10-29 14:30:08 -0700546 abi: abi,
David Tolnay35902302016-10-06 01:11:08 -0700547 items: items,
548 }),
549 })
550 ));
551
552 named!(foreign_item -> ForeignItem, alt!(
553 foreign_fn
554 |
555 foreign_static
556 ));
557
558 named!(foreign_fn -> ForeignItem, do_parse!(
559 attrs: many0!(outer_attr) >>
560 vis: visibility >>
561 keyword!("fn") >>
562 name: ident >>
563 generics: generics >>
564 punct!("(") >>
David Tolnay292e6002016-10-29 22:03:51 -0700565 inputs: separated_list!(punct!(","), fn_arg) >>
566 trailing_comma: option!(punct!(",")) >>
567 variadic: option!(cond_reduce!(trailing_comma.is_some(), punct!("..."))) >>
David Tolnay35902302016-10-06 01:11:08 -0700568 punct!(")") >>
569 ret: option!(preceded!(punct!("->"), ty)) >>
570 where_clause: where_clause >>
571 punct!(";") >>
572 (ForeignItem {
573 ident: name,
574 attrs: attrs,
575 node: ForeignItemKind::Fn(
576 Box::new(FnDecl {
577 inputs: inputs,
578 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700579 variadic: variadic.is_some(),
David Tolnay35902302016-10-06 01:11:08 -0700580 }),
581 Generics {
582 where_clause: where_clause,
583 .. generics
584 },
585 ),
586 vis: vis,
587 })
588 ));
589
590 named!(foreign_static -> ForeignItem, do_parse!(
591 attrs: many0!(outer_attr) >>
592 vis: visibility >>
593 keyword!("static") >>
594 mutability: mutability >>
595 id: ident >>
596 punct!(":") >>
597 ty: ty >>
598 punct!(";") >>
599 (ForeignItem {
600 ident: id,
601 attrs: attrs,
602 node: ForeignItemKind::Static(Box::new(ty), mutability),
603 vis: vis,
604 })
605 ));
606
David Tolnay3cf52982016-10-01 17:11:37 -0700607 named!(item_ty -> Item, do_parse!(
608 attrs: many0!(outer_attr) >>
609 vis: visibility >>
610 keyword!("type") >>
611 id: ident >>
612 generics: generics >>
613 punct!("=") >>
614 ty: ty >>
615 punct!(";") >>
616 (Item {
617 ident: id,
618 vis: vis,
619 attrs: attrs,
620 node: ItemKind::Ty(Box::new(ty), generics),
621 })
622 ));
623
David Tolnaya96a3fa2016-09-24 07:17:42 -0700624 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700625 macro_input,
626 |def: MacroInput| Item {
627 ident: def.ident,
628 vis: def.vis,
629 attrs: def.attrs,
630 node: match def.body {
631 Body::Enum(variants) => {
632 ItemKind::Enum(variants, def.generics)
633 }
634 Body::Struct(variant_data) => {
635 ItemKind::Struct(variant_data, def.generics)
636 }
637 }
638 }
639 ));
David Tolnay42602292016-10-01 22:25:45 -0700640
David Tolnay2f9fa632016-10-03 22:08:48 -0700641 named!(item_union -> Item, do_parse!(
642 attrs: many0!(outer_attr) >>
643 vis: visibility >>
644 keyword!("union") >>
645 id: ident >>
646 generics: generics >>
647 where_clause: where_clause >>
648 fields: struct_like_body >>
649 (Item {
650 ident: id,
651 vis: vis,
652 attrs: attrs,
653 node: ItemKind::Union(
654 VariantData::Struct(fields),
655 Generics {
656 where_clause: where_clause,
657 .. generics
658 },
659 ),
660 })
661 ));
662
David Tolnay0aecb732016-10-03 23:03:50 -0700663 named!(item_trait -> Item, do_parse!(
664 attrs: many0!(outer_attr) >>
665 vis: visibility >>
666 unsafety: unsafety >>
667 keyword!("trait") >>
668 id: ident >>
669 generics: generics >>
670 bounds: opt_vec!(preceded!(
671 punct!(":"),
672 separated_nonempty_list!(punct!("+"), ty_param_bound)
673 )) >>
674 where_clause: where_clause >>
675 punct!("{") >>
676 body: many0!(trait_item) >>
677 punct!("}") >>
678 (Item {
679 ident: id,
680 vis: vis,
681 attrs: attrs,
682 node: ItemKind::Trait(
683 unsafety,
684 Generics {
685 where_clause: where_clause,
686 .. generics
687 },
688 bounds,
689 body,
690 ),
691 })
692 ));
693
David Tolnayf94e2362016-10-04 00:29:51 -0700694 named!(item_default_impl -> Item, do_parse!(
695 attrs: many0!(outer_attr) >>
696 unsafety: unsafety >>
697 keyword!("impl") >>
698 path: path >>
699 keyword!("for") >>
700 punct!("..") >>
701 punct!("{") >>
702 punct!("}") >>
703 (Item {
704 ident: "".into(),
705 vis: Visibility::Inherited,
706 attrs: attrs,
707 node: ItemKind::DefaultImpl(unsafety, path),
708 })
709 ));
710
David Tolnay0aecb732016-10-03 23:03:50 -0700711 named!(trait_item -> TraitItem, alt!(
712 trait_item_const
713 |
714 trait_item_method
715 |
716 trait_item_type
717 |
718 trait_item_mac
719 ));
720
721 named!(trait_item_const -> TraitItem, do_parse!(
722 attrs: many0!(outer_attr) >>
723 keyword!("const") >>
724 id: ident >>
725 punct!(":") >>
726 ty: ty >>
727 value: option!(preceded!(punct!("="), expr)) >>
728 punct!(";") >>
729 (TraitItem {
730 ident: id,
731 attrs: attrs,
732 node: TraitItemKind::Const(ty, value),
733 })
734 ));
735
736 named!(trait_item_method -> TraitItem, do_parse!(
737 attrs: many0!(outer_attr) >>
738 constness: constness >>
739 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700740 abi: option!(abi) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700741 keyword!("fn") >>
742 name: ident >>
743 generics: generics >>
744 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700745 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700746 punct!(")") >>
747 ret: option!(preceded!(punct!("->"), ty)) >>
748 where_clause: where_clause >>
749 body: option!(block) >>
750 cond!(body.is_none(), punct!(";")) >>
751 (TraitItem {
752 ident: name,
753 attrs: attrs,
754 node: TraitItemKind::Method(
755 MethodSig {
756 unsafety: unsafety,
757 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700758 abi: abi,
David Tolnay0aecb732016-10-03 23:03:50 -0700759 decl: FnDecl {
760 inputs: inputs,
761 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700762 variadic: false,
David Tolnay0aecb732016-10-03 23:03:50 -0700763 },
764 generics: Generics {
765 where_clause: where_clause,
766 .. generics
767 },
768 },
769 body,
770 ),
771 })
772 ));
773
774 named!(trait_item_type -> TraitItem, do_parse!(
775 attrs: many0!(outer_attr) >>
776 keyword!("type") >>
777 id: ident >>
778 bounds: opt_vec!(preceded!(
779 punct!(":"),
780 separated_nonempty_list!(punct!("+"), ty_param_bound)
781 )) >>
782 default: option!(preceded!(punct!("="), ty)) >>
David Tolnayca085422016-10-04 00:12:38 -0700783 punct!(";") >>
David Tolnay0aecb732016-10-03 23:03:50 -0700784 (TraitItem {
785 ident: id,
786 attrs: attrs,
787 node: TraitItemKind::Type(bounds, default),
788 })
789 ));
790
791 named!(trait_item_mac -> TraitItem, do_parse!(
792 attrs: many0!(outer_attr) >>
793 id: ident >>
794 punct!("!") >>
795 body: delimited >>
David Tolnaye3198932016-10-04 00:21:34 -0700796 cond!(match body.delim {
797 DelimToken::Paren | DelimToken::Bracket => true,
798 DelimToken::Brace => false,
799 }, punct!(";")) >>
David Tolnay0aecb732016-10-03 23:03:50 -0700800 (TraitItem {
801 ident: id.clone(),
802 attrs: attrs,
803 node: TraitItemKind::Macro(Mac {
804 path: id.into(),
805 tts: vec![TokenTree::Delimited(body)],
806 }),
807 })
808 ));
809
David Tolnay4c9be372016-10-06 00:47:37 -0700810 named!(item_impl -> Item, do_parse!(
811 attrs: many0!(outer_attr) >>
812 unsafety: unsafety >>
813 keyword!("impl") >>
814 generics: generics >>
815 polarity_path: alt!(
816 do_parse!(
817 polarity: impl_polarity >>
818 path: path >>
819 keyword!("for") >>
820 ((polarity, Some(path)))
821 )
822 |
823 epsilon!() => { |_| (ImplPolarity::Positive, None) }
824 ) >>
825 self_ty: ty >>
826 where_clause: where_clause >>
827 punct!("{") >>
828 body: many0!(impl_item) >>
829 punct!("}") >>
830 (Item {
831 ident: "".into(),
832 vis: Visibility::Inherited,
833 attrs: attrs,
834 node: ItemKind::Impl(
835 unsafety,
836 polarity_path.0,
837 Generics {
838 where_clause: where_clause,
839 .. generics
840 },
841 polarity_path.1,
842 Box::new(self_ty),
843 body,
844 ),
845 })
846 ));
847
848 named!(impl_item -> ImplItem, alt!(
849 impl_item_const
850 |
851 impl_item_method
852 |
853 impl_item_type
854 |
855 impl_item_macro
856 ));
857
858 named!(impl_item_const -> ImplItem, do_parse!(
859 attrs: many0!(outer_attr) >>
860 vis: visibility >>
861 defaultness: defaultness >>
862 keyword!("const") >>
863 id: ident >>
864 punct!(":") >>
865 ty: ty >>
866 punct!("=") >>
867 value: expr >>
868 punct!(";") >>
869 (ImplItem {
870 ident: id,
871 vis: vis,
872 defaultness: defaultness,
873 attrs: attrs,
874 node: ImplItemKind::Const(ty, value),
875 })
876 ));
877
878 named!(impl_item_method -> ImplItem, do_parse!(
879 attrs: many0!(outer_attr) >>
880 vis: visibility >>
881 defaultness: defaultness >>
882 constness: constness >>
883 unsafety: unsafety >>
David Tolnayb8d8ef52016-10-29 14:30:08 -0700884 abi: option!(abi) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700885 keyword!("fn") >>
886 name: ident >>
887 generics: generics >>
888 punct!("(") >>
David Tolnayff46fd22016-10-08 13:53:28 -0700889 inputs: terminated_list!(punct!(","), fn_arg) >>
David Tolnay4c9be372016-10-06 00:47:37 -0700890 punct!(")") >>
891 ret: option!(preceded!(punct!("->"), ty)) >>
892 where_clause: where_clause >>
893 body: block >>
894 (ImplItem {
895 ident: name,
896 vis: vis,
897 defaultness: defaultness,
898 attrs: attrs,
899 node: ImplItemKind::Method(
900 MethodSig {
901 unsafety: unsafety,
902 constness: constness,
David Tolnayb8d8ef52016-10-29 14:30:08 -0700903 abi: abi,
David Tolnay4c9be372016-10-06 00:47:37 -0700904 decl: FnDecl {
905 inputs: inputs,
906 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
David Tolnay292e6002016-10-29 22:03:51 -0700907 variadic: false,
David Tolnay4c9be372016-10-06 00:47:37 -0700908 },
909 generics: Generics {
910 where_clause: where_clause,
911 .. generics
912 },
913 },
914 body,
915 ),
916 })
917 ));
918
919 named!(impl_item_type -> ImplItem, do_parse!(
920 attrs: many0!(outer_attr) >>
921 vis: visibility >>
922 defaultness: defaultness >>
923 keyword!("type") >>
924 id: ident >>
925 punct!("=") >>
926 ty: ty >>
927 punct!(";") >>
928 (ImplItem {
929 ident: id,
930 vis: vis,
931 defaultness: defaultness,
932 attrs: attrs,
933 node: ImplItemKind::Type(ty),
934 })
935 ));
936
937 named!(impl_item_macro -> ImplItem, do_parse!(
938 attrs: many0!(outer_attr) >>
939 id: ident >>
940 punct!("!") >>
941 body: delimited >>
942 cond!(match body.delim {
943 DelimToken::Paren | DelimToken::Bracket => true,
944 DelimToken::Brace => false,
945 }, punct!(";")) >>
946 (ImplItem {
947 ident: id.clone(),
948 vis: Visibility::Inherited,
949 defaultness: Defaultness::Final,
950 attrs: attrs,
951 node: ImplItemKind::Macro(Mac {
952 path: id.into(),
953 tts: vec![TokenTree::Delimited(body)],
954 }),
955 })
956 ));
957
958 named!(impl_polarity -> ImplPolarity, alt!(
959 punct!("!") => { |_| ImplPolarity::Negative }
960 |
961 epsilon!() => { |_| ImplPolarity::Positive }
962 ));
963
David Tolnay42602292016-10-01 22:25:45 -0700964 named!(constness -> Constness, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700965 keyword!("const") => { |_| Constness::Const }
David Tolnay42602292016-10-01 22:25:45 -0700966 |
967 epsilon!() => { |_| Constness::NotConst }
968 ));
969
David Tolnay4c9be372016-10-06 00:47:37 -0700970 named!(defaultness -> Defaultness, alt!(
971 keyword!("default") => { |_| Defaultness::Default }
972 |
973 epsilon!() => { |_| Defaultness::Final }
974 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700975}
David Tolnay4a51dc72016-10-01 00:40:31 -0700976
977#[cfg(feature = "printing")]
978mod printing {
979 use super::*;
David Tolnaycc3d66e2016-10-02 23:36:05 -0700980 use {Delimited, DelimToken, FunctionRetTy, TokenTree};
David Tolnay4a51dc72016-10-01 00:40:31 -0700981 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700982 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700983 use quote::{Tokens, ToTokens};
984
985 impl ToTokens for Item {
986 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -0700987 tokens.append_all(self.attrs.outer());
David Tolnay4a51dc72016-10-01 00:40:31 -0700988 match self.node {
989 ItemKind::ExternCrate(ref original) => {
990 tokens.append("extern");
991 tokens.append("crate");
992 if let Some(ref original) = *original {
993 original.to_tokens(tokens);
994 tokens.append("as");
995 }
996 self.ident.to_tokens(tokens);
997 tokens.append(";");
998 }
David Tolnay4a057422016-10-08 00:02:31 -0700999 ItemKind::Use(ref view_path) => {
1000 self.vis.to_tokens(tokens);
1001 tokens.append("use");
1002 view_path.to_tokens(tokens);
1003 tokens.append(";");
1004 }
David Tolnay47a877c2016-10-01 16:50:55 -07001005 ItemKind::Static(ref ty, ref mutability, ref expr) => {
1006 self.vis.to_tokens(tokens);
1007 tokens.append("static");
1008 mutability.to_tokens(tokens);
1009 self.ident.to_tokens(tokens);
1010 tokens.append(":");
1011 ty.to_tokens(tokens);
1012 tokens.append("=");
1013 expr.to_tokens(tokens);
1014 tokens.append(";");
1015 }
1016 ItemKind::Const(ref ty, ref expr) => {
1017 self.vis.to_tokens(tokens);
1018 tokens.append("const");
1019 self.ident.to_tokens(tokens);
1020 tokens.append(":");
1021 ty.to_tokens(tokens);
1022 tokens.append("=");
1023 expr.to_tokens(tokens);
1024 tokens.append(";");
1025 }
David Tolnay42602292016-10-01 22:25:45 -07001026 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
1027 self.vis.to_tokens(tokens);
1028 constness.to_tokens(tokens);
1029 unsafety.to_tokens(tokens);
1030 abi.to_tokens(tokens);
1031 tokens.append("fn");
1032 self.ident.to_tokens(tokens);
1033 generics.to_tokens(tokens);
David Tolnay62f374c2016-10-02 13:37:00 -07001034 tokens.append("(");
1035 tokens.append_separated(&decl.inputs, ",");
1036 tokens.append(")");
1037 if let FunctionRetTy::Ty(ref ty) = decl.output {
1038 tokens.append("->");
1039 ty.to_tokens(tokens);
1040 }
David Tolnay42602292016-10-01 22:25:45 -07001041 generics.where_clause.to_tokens(tokens);
1042 block.to_tokens(tokens);
1043 }
David Tolnay35902302016-10-06 01:11:08 -07001044 ItemKind::Mod(ref items) => {
1045 self.vis.to_tokens(tokens);
1046 tokens.append("mod");
1047 self.ident.to_tokens(tokens);
David Tolnay37d10332016-10-13 20:51:04 -07001048 match *items {
1049 Some(ref items) => {
1050 tokens.append("{");
David Tolnay7b8009b2016-10-25 22:36:00 -07001051 tokens.append_all(self.attrs.inner());
David Tolnay37d10332016-10-13 20:51:04 -07001052 tokens.append_all(items);
1053 tokens.append("}");
1054 }
1055 None => tokens.append(";"),
1056 }
David Tolnay35902302016-10-06 01:11:08 -07001057 }
1058 ItemKind::ForeignMod(ref foreign_mod) => {
1059 self.vis.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -07001060 foreign_mod.abi.to_tokens(tokens);
David Tolnay35902302016-10-06 01:11:08 -07001061 tokens.append("{");
1062 tokens.append_all(&foreign_mod.items);
1063 tokens.append("}");
1064 }
David Tolnay3cf52982016-10-01 17:11:37 -07001065 ItemKind::Ty(ref ty, ref generics) => {
1066 self.vis.to_tokens(tokens);
1067 tokens.append("type");
1068 self.ident.to_tokens(tokens);
1069 generics.to_tokens(tokens);
1070 tokens.append("=");
1071 ty.to_tokens(tokens);
1072 tokens.append(";");
1073 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001074 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001075 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001076 tokens.append("enum");
1077 self.ident.to_tokens(tokens);
1078 generics.to_tokens(tokens);
1079 generics.where_clause.to_tokens(tokens);
1080 tokens.append("{");
1081 for variant in variants {
1082 variant.to_tokens(tokens);
1083 tokens.append(",");
1084 }
1085 tokens.append("}");
1086 }
1087 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -07001088 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001089 tokens.append("struct");
1090 self.ident.to_tokens(tokens);
1091 generics.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -07001092 match *variant_data {
David Tolnaydaaf7742016-10-03 11:11:43 -07001093 VariantData::Struct(_) => {
David Tolnay28c1db62016-10-27 22:48:18 -07001094 generics.where_clause.to_tokens(tokens);
1095 variant_data.to_tokens(tokens);
David Tolnaydaaf7742016-10-03 11:11:43 -07001096 // no semicolon
1097 }
David Tolnay28c1db62016-10-27 22:48:18 -07001098 VariantData::Tuple(_) => {
1099 variant_data.to_tokens(tokens);
1100 generics.where_clause.to_tokens(tokens);
1101 tokens.append(";");
1102 }
1103 VariantData::Unit => {
1104 generics.where_clause.to_tokens(tokens);
1105 tokens.append(";");
1106 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001107 }
1108 }
David Tolnay2f9fa632016-10-03 22:08:48 -07001109 ItemKind::Union(ref variant_data, ref generics) => {
1110 self.vis.to_tokens(tokens);
1111 tokens.append("union");
1112 self.ident.to_tokens(tokens);
1113 generics.to_tokens(tokens);
1114 generics.where_clause.to_tokens(tokens);
1115 variant_data.to_tokens(tokens);
1116 }
David Tolnayca085422016-10-04 00:12:38 -07001117 ItemKind::Trait(unsafety, ref generics, ref bound, ref items) => {
1118 self.vis.to_tokens(tokens);
1119 unsafety.to_tokens(tokens);
1120 tokens.append("trait");
1121 self.ident.to_tokens(tokens);
David Tolnaye4606332016-10-25 21:57:41 -07001122 generics.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001123 if !bound.is_empty() {
1124 tokens.append(":");
1125 tokens.append_separated(bound, "+");
1126 }
David Tolnayca085422016-10-04 00:12:38 -07001127 generics.where_clause.to_tokens(tokens);
1128 tokens.append("{");
1129 tokens.append_all(items);
1130 tokens.append("}");
1131 }
David Tolnayf94e2362016-10-04 00:29:51 -07001132 ItemKind::DefaultImpl(unsafety, ref path) => {
1133 unsafety.to_tokens(tokens);
1134 tokens.append("impl");
1135 path.to_tokens(tokens);
1136 tokens.append("for");
1137 tokens.append("..");
1138 tokens.append("{");
1139 tokens.append("}");
1140 }
David Tolnay3bcfb722016-10-08 11:58:36 -07001141 ItemKind::Impl(unsafety, polarity, ref generics, ref path, ref ty, ref items) => {
David Tolnay4c9be372016-10-06 00:47:37 -07001142 unsafety.to_tokens(tokens);
1143 tokens.append("impl");
1144 generics.to_tokens(tokens);
1145 if let Some(ref path) = *path {
1146 polarity.to_tokens(tokens);
1147 path.to_tokens(tokens);
1148 tokens.append("for");
1149 }
1150 ty.to_tokens(tokens);
1151 generics.where_clause.to_tokens(tokens);
1152 tokens.append("{");
1153 tokens.append_all(items);
1154 tokens.append("}");
1155 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001156 ItemKind::Mac(ref mac) => {
1157 mac.path.to_tokens(tokens);
1158 tokens.append("!");
1159 self.ident.to_tokens(tokens);
1160 for tt in &mac.tts {
1161 tt.to_tokens(tokens);
1162 }
1163 match mac.tts.last() {
David Tolnaydaaf7742016-10-03 11:11:43 -07001164 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1165 // no semicolon
1166 }
David Tolnaycc3d66e2016-10-02 23:36:05 -07001167 _ => tokens.append(";"),
1168 }
1169 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001170 }
1171 }
1172 }
David Tolnay42602292016-10-01 22:25:45 -07001173
David Tolnay4a057422016-10-08 00:02:31 -07001174 impl ToTokens for ViewPath {
1175 fn to_tokens(&self, tokens: &mut Tokens) {
1176 match *self {
1177 ViewPath::Simple(ref path, ref rename) => {
1178 path.to_tokens(tokens);
1179 if let Some(ref rename) = *rename {
1180 tokens.append("as");
1181 rename.to_tokens(tokens);
1182 }
1183 }
1184 ViewPath::Glob(ref path) => {
1185 path.to_tokens(tokens);
1186 tokens.append("::");
1187 tokens.append("*");
1188 }
1189 ViewPath::List(ref path, ref items) => {
1190 path.to_tokens(tokens);
David Tolnay12417832016-10-08 00:12:37 -07001191 if path.global || !path.segments.is_empty() {
1192 tokens.append("::");
1193 }
David Tolnay4a057422016-10-08 00:02:31 -07001194 tokens.append("{");
1195 tokens.append_separated(items, ",");
1196 tokens.append("}");
1197 }
1198 }
1199 }
1200 }
1201
1202 impl ToTokens for PathListItem {
1203 fn to_tokens(&self, tokens: &mut Tokens) {
1204 self.name.to_tokens(tokens);
1205 if let Some(ref rename) = self.rename {
1206 tokens.append("as");
1207 rename.to_tokens(tokens);
1208 }
1209 }
1210 }
1211
David Tolnayca085422016-10-04 00:12:38 -07001212 impl ToTokens for TraitItem {
1213 fn to_tokens(&self, tokens: &mut Tokens) {
1214 tokens.append_all(self.attrs.outer());
1215 match self.node {
1216 TraitItemKind::Const(ref ty, ref expr) => {
1217 tokens.append("const");
1218 self.ident.to_tokens(tokens);
1219 tokens.append(":");
1220 ty.to_tokens(tokens);
1221 if let Some(ref expr) = *expr {
1222 tokens.append("=");
1223 expr.to_tokens(tokens);
1224 }
1225 tokens.append(";");
1226 }
1227 TraitItemKind::Method(ref sig, ref block) => {
David Tolnayb31d3f02016-10-25 21:15:13 -07001228 sig.constness.to_tokens(tokens);
David Tolnayca085422016-10-04 00:12:38 -07001229 sig.unsafety.to_tokens(tokens);
1230 sig.abi.to_tokens(tokens);
1231 tokens.append("fn");
1232 self.ident.to_tokens(tokens);
1233 sig.generics.to_tokens(tokens);
1234 tokens.append("(");
1235 tokens.append_separated(&sig.decl.inputs, ",");
1236 tokens.append(")");
1237 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1238 tokens.append("->");
1239 ty.to_tokens(tokens);
1240 }
1241 sig.generics.where_clause.to_tokens(tokens);
1242 match *block {
1243 Some(ref block) => block.to_tokens(tokens),
1244 None => tokens.append(";"),
1245 }
1246 }
1247 TraitItemKind::Type(ref bound, ref default) => {
1248 tokens.append("type");
1249 self.ident.to_tokens(tokens);
1250 if !bound.is_empty() {
1251 tokens.append(":");
1252 tokens.append_separated(bound, "+");
1253 }
1254 if let Some(ref default) = *default {
1255 tokens.append("=");
1256 default.to_tokens(tokens);
1257 }
1258 tokens.append(";");
1259 }
1260 TraitItemKind::Macro(ref mac) => {
1261 mac.to_tokens(tokens);
David Tolnaye3198932016-10-04 00:21:34 -07001262 match mac.tts.last() {
1263 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1264 // no semicolon
1265 }
1266 _ => tokens.append(";"),
1267 }
David Tolnayca085422016-10-04 00:12:38 -07001268 }
1269 }
1270 }
1271 }
1272
David Tolnay4c9be372016-10-06 00:47:37 -07001273 impl ToTokens for ImplItem {
1274 fn to_tokens(&self, tokens: &mut Tokens) {
1275 tokens.append_all(self.attrs.outer());
1276 match self.node {
1277 ImplItemKind::Const(ref ty, ref expr) => {
1278 self.vis.to_tokens(tokens);
1279 self.defaultness.to_tokens(tokens);
1280 tokens.append("const");
1281 self.ident.to_tokens(tokens);
1282 tokens.append(":");
1283 ty.to_tokens(tokens);
1284 tokens.append("=");
1285 expr.to_tokens(tokens);
1286 tokens.append(";");
1287 }
1288 ImplItemKind::Method(ref sig, ref block) => {
1289 self.vis.to_tokens(tokens);
1290 self.defaultness.to_tokens(tokens);
David Tolnayb31d3f02016-10-25 21:15:13 -07001291 sig.constness.to_tokens(tokens);
David Tolnay4c9be372016-10-06 00:47:37 -07001292 sig.unsafety.to_tokens(tokens);
1293 sig.abi.to_tokens(tokens);
1294 tokens.append("fn");
1295 self.ident.to_tokens(tokens);
1296 sig.generics.to_tokens(tokens);
1297 tokens.append("(");
1298 tokens.append_separated(&sig.decl.inputs, ",");
1299 tokens.append(")");
1300 if let FunctionRetTy::Ty(ref ty) = sig.decl.output {
1301 tokens.append("->");
1302 ty.to_tokens(tokens);
1303 }
1304 sig.generics.where_clause.to_tokens(tokens);
1305 block.to_tokens(tokens);
1306 }
1307 ImplItemKind::Type(ref ty) => {
1308 self.vis.to_tokens(tokens);
1309 self.defaultness.to_tokens(tokens);
1310 tokens.append("type");
1311 self.ident.to_tokens(tokens);
1312 tokens.append("=");
1313 ty.to_tokens(tokens);
1314 tokens.append(";");
1315 }
1316 ImplItemKind::Macro(ref mac) => {
1317 mac.to_tokens(tokens);
1318 match mac.tts.last() {
1319 Some(&TokenTree::Delimited(Delimited { delim: DelimToken::Brace, .. })) => {
1320 // no semicolon
1321 }
1322 _ => tokens.append(";"),
1323 }
1324 }
1325 }
1326 }
1327 }
1328
David Tolnay35902302016-10-06 01:11:08 -07001329 impl ToTokens for ForeignItem {
1330 fn to_tokens(&self, tokens: &mut Tokens) {
1331 tokens.append_all(self.attrs.outer());
1332 match self.node {
1333 ForeignItemKind::Fn(ref decl, ref generics) => {
1334 self.vis.to_tokens(tokens);
1335 tokens.append("fn");
1336 self.ident.to_tokens(tokens);
1337 generics.to_tokens(tokens);
1338 tokens.append("(");
1339 tokens.append_separated(&decl.inputs, ",");
David Tolnay292e6002016-10-29 22:03:51 -07001340 if decl.variadic {
1341 if !decl.inputs.is_empty() {
1342 tokens.append(",");
1343 }
1344 tokens.append("...");
1345 }
David Tolnay35902302016-10-06 01:11:08 -07001346 tokens.append(")");
1347 if let FunctionRetTy::Ty(ref ty) = decl.output {
1348 tokens.append("->");
1349 ty.to_tokens(tokens);
1350 }
1351 generics.where_clause.to_tokens(tokens);
1352 tokens.append(";");
1353 }
1354 ForeignItemKind::Static(ref ty, mutability) => {
1355 self.vis.to_tokens(tokens);
1356 tokens.append("static");
1357 mutability.to_tokens(tokens);
1358 self.ident.to_tokens(tokens);
1359 tokens.append(":");
1360 ty.to_tokens(tokens);
1361 tokens.append(";");
1362 }
1363 }
1364 }
1365 }
1366
David Tolnay62f374c2016-10-02 13:37:00 -07001367 impl ToTokens for FnArg {
1368 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnayca085422016-10-04 00:12:38 -07001369 match *self {
1370 FnArg::SelfRef(ref lifetime, mutability) => {
1371 tokens.append("&");
1372 lifetime.to_tokens(tokens);
1373 mutability.to_tokens(tokens);
1374 tokens.append("self");
1375 }
1376 FnArg::SelfValue(mutability) => {
1377 mutability.to_tokens(tokens);
1378 tokens.append("self");
1379 }
1380 FnArg::Captured(ref pat, ref ty) => {
1381 pat.to_tokens(tokens);
1382 tokens.append(":");
1383 ty.to_tokens(tokens);
1384 }
1385 FnArg::Ignored(ref ty) => {
1386 ty.to_tokens(tokens);
1387 }
1388 }
David Tolnay62f374c2016-10-02 13:37:00 -07001389 }
1390 }
1391
David Tolnay42602292016-10-01 22:25:45 -07001392 impl ToTokens for Constness {
1393 fn to_tokens(&self, tokens: &mut Tokens) {
1394 match *self {
1395 Constness::Const => tokens.append("const"),
David Tolnaydaaf7742016-10-03 11:11:43 -07001396 Constness::NotConst => {
1397 // nothing
1398 }
David Tolnay42602292016-10-01 22:25:45 -07001399 }
1400 }
1401 }
1402
David Tolnay4c9be372016-10-06 00:47:37 -07001403 impl ToTokens for Defaultness {
1404 fn to_tokens(&self, tokens: &mut Tokens) {
1405 match *self {
1406 Defaultness::Default => tokens.append("default"),
1407 Defaultness::Final => {
1408 // nothing
1409 }
1410 }
1411 }
1412 }
1413
1414 impl ToTokens for ImplPolarity {
1415 fn to_tokens(&self, tokens: &mut Tokens) {
1416 match *self {
1417 ImplPolarity::Negative => tokens.append("!"),
1418 ImplPolarity::Positive => {
1419 // nothing
1420 }
1421 }
1422 }
1423 }
David Tolnay4a51dc72016-10-01 00:40:31 -07001424}