blob: 85924858c53d60350b160e550ea057ccf4db21da [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>),
64 // Default trait implementation.
65 ///
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,
72 ImplPolarity,
73 Generics,
74 Option<Path>, // (optional) trait this impl implements
75 Box<Ty>, // self
76 Vec<ImplItem>),
77 /// 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 Tolnayaed77b02016-09-23 20:50:31 -070090 Simple(Ident, Path),
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}`
96 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///
129/// E.g. `extern { .. }` or `extern C { .. }`
130#[derive(Debug, Clone, Eq, PartialEq)]
131pub struct ForeignMod {
132 pub abi: Abi,
133 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),
149 /// A foreign static item (`static ext: u8`), with optional mutability
150 /// (the boolean is true when mutable)
151 Static(Box<Ty>, bool),
152}
153
154/// Represents an item declaration within a trait declaration,
155/// possibly including a default implementation. A trait item is
156/// either required (meaning it doesn't have an implementation, just a
157/// signature) or provided (meaning it has a default implementation).
158#[derive(Debug, Clone, Eq, PartialEq)]
159pub struct TraitItem {
160 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700161 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700162 pub node: TraitItemKind,
163}
164
165#[derive(Debug, Clone, Eq, PartialEq)]
166pub enum TraitItemKind {
167 Const(Ty, Option<Expr>),
168 Method(MethodSig, Option<Block>),
169 Type(Vec<TyParamBound>, Option<Ty>),
170 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700171}
172
David Tolnay55337722016-09-11 12:58:56 -0700173#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700174pub enum ImplPolarity {
175 /// `impl Trait for Type`
176 Positive,
177 /// `impl !Trait for Type`
178 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700179}
180
David Tolnayf38cdf62016-09-23 19:07:09 -0700181#[derive(Debug, Clone, Eq, PartialEq)]
182pub struct ImplItem {
183 pub ident: Ident,
184 pub vis: Visibility,
185 pub defaultness: Defaultness,
186 pub attrs: Vec<Attribute>,
187 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700188}
189
David Tolnayf38cdf62016-09-23 19:07:09 -0700190#[derive(Debug, Clone, Eq, PartialEq)]
191pub enum ImplItemKind {
192 Const(Ty, Expr),
193 Method(MethodSig, Block),
194 Type(Ty),
195 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700196}
David Tolnayd5025812016-09-04 14:21:46 -0700197
David Tolnayf38cdf62016-09-23 19:07:09 -0700198/// Represents a method's signature in a trait declaration,
199/// or in an implementation.
200#[derive(Debug, Clone, Eq, PartialEq)]
201pub struct MethodSig {
202 pub unsafety: Unsafety,
203 pub constness: Constness,
204 pub abi: Abi,
205 pub decl: FnDecl,
206 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700207}
David Tolnayedf2b992016-09-23 20:43:45 -0700208
209#[cfg(feature = "parsing")]
210pub mod parsing {
211 use super::*;
David Tolnay42602292016-10-01 22:25:45 -0700212 use {FnDecl, FunctionRetTy, Generics};
David Tolnay4a51dc72016-10-01 00:40:31 -0700213 use attr::parsing::outer_attr;
David Tolnayedf2b992016-09-23 20:43:45 -0700214 use data::parsing::visibility;
David Tolnay42602292016-10-01 22:25:45 -0700215 use expr::parsing::{block, expr};
216 use generics::parsing::{generics, where_clause};
David Tolnayedf2b992016-09-23 20:43:45 -0700217 use ident::parsing::ident;
David Tolnay42602292016-10-01 22:25:45 -0700218 use lit::parsing::quoted_string;
David Tolnayedf2b992016-09-23 20:43:45 -0700219 use macro_input::{Body, MacroInput};
220 use macro_input::parsing::macro_input;
David Tolnay42602292016-10-01 22:25:45 -0700221 use ty::parsing::{fn_arg, mutability, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700222
223 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700224 item_extern_crate
225 // TODO: Use
David Tolnay47a877c2016-10-01 16:50:55 -0700226 |
227 item_static
228 |
229 item_const
David Tolnay42602292016-10-01 22:25:45 -0700230 |
231 item_fn
David Tolnaya96a3fa2016-09-24 07:17:42 -0700232 // TODO: Mod
233 // TODO: ForeignMod
David Tolnay3cf52982016-10-01 17:11:37 -0700234 |
235 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700236 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700237 item_struct_or_enum
238 // TODO: Union
239 // TODO: Trait
240 // TODO: DefaultImpl
241 // TODO: Impl
242 // TODO: Mac
David Tolnayedf2b992016-09-23 20:43:45 -0700243 ));
244
David Tolnaya96a3fa2016-09-24 07:17:42 -0700245 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700246 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700247 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700248 keyword!("extern") >>
249 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700250 id: ident >>
251 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700252 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700253 ident
254 )) >>
255 punct!(";") >>
256 ({
257 let (name, original_name) = match rename {
258 Some(rename) => (rename, Some(id)),
259 None => (id, None),
260 };
261 Item {
262 ident: name,
263 vis: vis,
264 attrs: attrs,
265 node: ItemKind::ExternCrate(original_name),
266 }
267 })
268 ));
269
David Tolnay47a877c2016-10-01 16:50:55 -0700270 named!(item_static -> Item, do_parse!(
271 attrs: many0!(outer_attr) >>
272 vis: visibility >>
273 keyword!("static") >>
274 mutability: mutability >>
275 id: ident >>
276 punct!(":") >>
277 ty: ty >>
278 punct!("=") >>
279 value: expr >>
280 punct!(";") >>
281 (Item {
282 ident: id,
283 vis: vis,
284 attrs: attrs,
285 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
286 })
287 ));
288
289 named!(item_const -> Item, do_parse!(
290 attrs: many0!(outer_attr) >>
291 vis: visibility >>
292 keyword!("const") >>
293 id: ident >>
294 punct!(":") >>
295 ty: ty >>
296 punct!("=") >>
297 value: expr >>
298 punct!(";") >>
299 (Item {
300 ident: id,
301 vis: vis,
302 attrs: attrs,
303 node: ItemKind::Const(Box::new(ty), Box::new(value)),
304 })
305 ));
306
David Tolnay42602292016-10-01 22:25:45 -0700307 named!(item_fn -> Item, do_parse!(
308 attrs: many0!(outer_attr) >>
309 vis: visibility >>
310 constness: constness >>
311 unsafety: unsafety >>
312 abi: option!(preceded!(keyword!("extern"), quoted_string)) >>
313 keyword!("fn") >>
314 name: ident >>
315 generics: generics >>
316 punct!("(") >>
317 inputs: separated_list!(punct!(","), fn_arg) >>
318 punct!(")") >>
319 ret: option!(preceded!(punct!("->"), ty)) >>
320 where_clause: where_clause >>
321 body: block >>
322 (Item {
323 ident: name,
324 vis: vis,
325 attrs: attrs,
326 node: ItemKind::Fn(
327 Box::new(FnDecl {
328 inputs: inputs,
329 output: ret.map(FunctionRetTy::Ty).unwrap_or(FunctionRetTy::Default),
330 }),
331 unsafety,
332 constness,
333 abi.map(Abi),
334 Generics {
335 where_clause: where_clause,
336 .. generics
337 },
338 Box::new(body),
339 ),
340 })
341 ));
342
David Tolnay3cf52982016-10-01 17:11:37 -0700343 named!(item_ty -> Item, do_parse!(
344 attrs: many0!(outer_attr) >>
345 vis: visibility >>
346 keyword!("type") >>
347 id: ident >>
348 generics: generics >>
349 punct!("=") >>
350 ty: ty >>
351 punct!(";") >>
352 (Item {
353 ident: id,
354 vis: vis,
355 attrs: attrs,
356 node: ItemKind::Ty(Box::new(ty), generics),
357 })
358 ));
359
David Tolnaya96a3fa2016-09-24 07:17:42 -0700360 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700361 macro_input,
362 |def: MacroInput| Item {
363 ident: def.ident,
364 vis: def.vis,
365 attrs: def.attrs,
366 node: match def.body {
367 Body::Enum(variants) => {
368 ItemKind::Enum(variants, def.generics)
369 }
370 Body::Struct(variant_data) => {
371 ItemKind::Struct(variant_data, def.generics)
372 }
373 }
374 }
375 ));
David Tolnay42602292016-10-01 22:25:45 -0700376
377 named!(constness -> Constness, alt!(
378 do_parse!(
379 keyword!("const") >>
380 (Constness::Const)
381 )
382 |
383 epsilon!() => { |_| Constness::NotConst }
384 ));
385
386 named!(unsafety -> Unsafety, alt!(
387 do_parse!(
388 keyword!("unsafe") >>
389 (Unsafety::Unsafe)
390 )
391 |
392 epsilon!() => { |_| Unsafety::Normal }
393 ));
David Tolnayedf2b992016-09-23 20:43:45 -0700394}
David Tolnay4a51dc72016-10-01 00:40:31 -0700395
396#[cfg(feature = "printing")]
397mod printing {
398 use super::*;
399 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700400 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700401 use quote::{Tokens, ToTokens};
402
403 impl ToTokens for Item {
404 fn to_tokens(&self, tokens: &mut Tokens) {
405 for attr in self.attrs.outer() {
406 attr.to_tokens(tokens);
407 }
408 match self.node {
409 ItemKind::ExternCrate(ref original) => {
410 tokens.append("extern");
411 tokens.append("crate");
412 if let Some(ref original) = *original {
413 original.to_tokens(tokens);
414 tokens.append("as");
415 }
416 self.ident.to_tokens(tokens);
417 tokens.append(";");
418 }
419 ItemKind::Use(ref _view_path) => unimplemented!(),
David Tolnay47a877c2016-10-01 16:50:55 -0700420 ItemKind::Static(ref ty, ref mutability, ref expr) => {
421 self.vis.to_tokens(tokens);
422 tokens.append("static");
423 mutability.to_tokens(tokens);
424 self.ident.to_tokens(tokens);
425 tokens.append(":");
426 ty.to_tokens(tokens);
427 tokens.append("=");
428 expr.to_tokens(tokens);
429 tokens.append(";");
430 }
431 ItemKind::Const(ref ty, ref expr) => {
432 self.vis.to_tokens(tokens);
433 tokens.append("const");
434 self.ident.to_tokens(tokens);
435 tokens.append(":");
436 ty.to_tokens(tokens);
437 tokens.append("=");
438 expr.to_tokens(tokens);
439 tokens.append(";");
440 }
David Tolnay42602292016-10-01 22:25:45 -0700441 ItemKind::Fn(ref decl, unsafety, constness, ref abi, ref generics, ref block) => {
442 self.vis.to_tokens(tokens);
443 constness.to_tokens(tokens);
444 unsafety.to_tokens(tokens);
445 abi.to_tokens(tokens);
446 tokens.append("fn");
447 self.ident.to_tokens(tokens);
448 generics.to_tokens(tokens);
449 decl.to_tokens(tokens);
450 generics.where_clause.to_tokens(tokens);
451 block.to_tokens(tokens);
452 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700453 ItemKind::Mod(ref _items) => unimplemented!(),
454 ItemKind::ForeignMod(ref _foreign_mod) => unimplemented!(),
David Tolnay3cf52982016-10-01 17:11:37 -0700455 ItemKind::Ty(ref ty, ref generics) => {
456 self.vis.to_tokens(tokens);
457 tokens.append("type");
458 self.ident.to_tokens(tokens);
459 generics.to_tokens(tokens);
460 tokens.append("=");
461 ty.to_tokens(tokens);
462 tokens.append(";");
463 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700464 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700465 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700466 tokens.append("enum");
467 self.ident.to_tokens(tokens);
468 generics.to_tokens(tokens);
469 generics.where_clause.to_tokens(tokens);
470 tokens.append("{");
471 for variant in variants {
472 variant.to_tokens(tokens);
473 tokens.append(",");
474 }
475 tokens.append("}");
476 }
477 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700478 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700479 tokens.append("struct");
480 self.ident.to_tokens(tokens);
481 generics.to_tokens(tokens);
482 generics.where_clause.to_tokens(tokens);
483 variant_data.to_tokens(tokens);
484 match *variant_data {
485 VariantData::Struct(_) => { /* no semicolon */ }
486 VariantData::Tuple(_) |
487 VariantData::Unit => tokens.append(";"),
488 }
489 }
490 ItemKind::Union(ref _variant_data, ref _generics) => unimplemented!(),
491 ItemKind::Trait(_unsafety, ref _generics, ref _bound, ref _item) => unimplemented!(),
492 ItemKind::DefaultImpl(_unsafety, ref _path) => unimplemented!(),
493 ItemKind::Impl(_unsafety, _polarity, ref _generics, ref _path, ref _ty, ref _item) => unimplemented!(),
494 ItemKind::Mac(ref _mac) => unimplemented!(),
495 }
496 }
497 }
David Tolnay42602292016-10-01 22:25:45 -0700498
499 impl ToTokens for Unsafety {
500 fn to_tokens(&self, tokens: &mut Tokens) {
501 match *self {
502 Unsafety::Unsafe => tokens.append("unsafe"),
503 Unsafety::Normal => { /* nothing */ },
504 }
505 }
506 }
507
508 impl ToTokens for Constness {
509 fn to_tokens(&self, tokens: &mut Tokens) {
510 match *self {
511 Constness::Const => tokens.append("const"),
512 Constness::NotConst => { /* nothing */ },
513 }
514 }
515 }
516
517 impl ToTokens for Abi {
518 fn to_tokens(&self, tokens: &mut Tokens) {
519 tokens.append("extern");
520 self.0.to_tokens(tokens);
521 }
522 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700523}