blob: 4d646fe0f1d658d7cdb93127ba70da694c078ef9 [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 { .. }`
35 Fn(Box<FnDecl>, Unsafety, Constness, Abi, Generics, Box<Block>),
36 /// 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 Tolnay4a51dc72016-10-01 00:40:31 -0700212 use attr::parsing::outer_attr;
David Tolnayedf2b992016-09-23 20:43:45 -0700213 use data::parsing::visibility;
David Tolnay47a877c2016-10-01 16:50:55 -0700214 use expr::parsing::expr;
David Tolnay3cf52982016-10-01 17:11:37 -0700215 use generics::parsing::generics;
David Tolnayedf2b992016-09-23 20:43:45 -0700216 use ident::parsing::ident;
217 use macro_input::{Body, MacroInput};
218 use macro_input::parsing::macro_input;
David Tolnay47a877c2016-10-01 16:50:55 -0700219 use ty::parsing::{mutability, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700220
221 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700222 item_extern_crate
223 // TODO: Use
David Tolnay47a877c2016-10-01 16:50:55 -0700224 |
225 item_static
226 |
227 item_const
David Tolnaya96a3fa2016-09-24 07:17:42 -0700228 // TODO: Fn
229 // TODO: Mod
230 // TODO: ForeignMod
David Tolnay3cf52982016-10-01 17:11:37 -0700231 |
232 item_ty
David Tolnayedf2b992016-09-23 20:43:45 -0700233 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700234 item_struct_or_enum
235 // TODO: Union
236 // TODO: Trait
237 // TODO: DefaultImpl
238 // TODO: Impl
239 // TODO: Mac
David Tolnayedf2b992016-09-23 20:43:45 -0700240 ));
241
David Tolnaya96a3fa2016-09-24 07:17:42 -0700242 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700243 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700244 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700245 keyword!("extern") >>
246 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700247 id: ident >>
248 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700249 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700250 ident
251 )) >>
252 punct!(";") >>
253 ({
254 let (name, original_name) = match rename {
255 Some(rename) => (rename, Some(id)),
256 None => (id, None),
257 };
258 Item {
259 ident: name,
260 vis: vis,
261 attrs: attrs,
262 node: ItemKind::ExternCrate(original_name),
263 }
264 })
265 ));
266
David Tolnay47a877c2016-10-01 16:50:55 -0700267 named!(item_static -> Item, do_parse!(
268 attrs: many0!(outer_attr) >>
269 vis: visibility >>
270 keyword!("static") >>
271 mutability: mutability >>
272 id: ident >>
273 punct!(":") >>
274 ty: ty >>
275 punct!("=") >>
276 value: expr >>
277 punct!(";") >>
278 (Item {
279 ident: id,
280 vis: vis,
281 attrs: attrs,
282 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
283 })
284 ));
285
286 named!(item_const -> Item, do_parse!(
287 attrs: many0!(outer_attr) >>
288 vis: visibility >>
289 keyword!("const") >>
290 id: ident >>
291 punct!(":") >>
292 ty: ty >>
293 punct!("=") >>
294 value: expr >>
295 punct!(";") >>
296 (Item {
297 ident: id,
298 vis: vis,
299 attrs: attrs,
300 node: ItemKind::Const(Box::new(ty), Box::new(value)),
301 })
302 ));
303
David Tolnay3cf52982016-10-01 17:11:37 -0700304 named!(item_ty -> Item, do_parse!(
305 attrs: many0!(outer_attr) >>
306 vis: visibility >>
307 keyword!("type") >>
308 id: ident >>
309 generics: generics >>
310 punct!("=") >>
311 ty: ty >>
312 punct!(";") >>
313 (Item {
314 ident: id,
315 vis: vis,
316 attrs: attrs,
317 node: ItemKind::Ty(Box::new(ty), generics),
318 })
319 ));
320
David Tolnaya96a3fa2016-09-24 07:17:42 -0700321 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700322 macro_input,
323 |def: MacroInput| Item {
324 ident: def.ident,
325 vis: def.vis,
326 attrs: def.attrs,
327 node: match def.body {
328 Body::Enum(variants) => {
329 ItemKind::Enum(variants, def.generics)
330 }
331 Body::Struct(variant_data) => {
332 ItemKind::Struct(variant_data, def.generics)
333 }
334 }
335 }
336 ));
337}
David Tolnay4a51dc72016-10-01 00:40:31 -0700338
339#[cfg(feature = "printing")]
340mod printing {
341 use super::*;
342 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700343 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700344 use quote::{Tokens, ToTokens};
345
346 impl ToTokens for Item {
347 fn to_tokens(&self, tokens: &mut Tokens) {
348 for attr in self.attrs.outer() {
349 attr.to_tokens(tokens);
350 }
351 match self.node {
352 ItemKind::ExternCrate(ref original) => {
353 tokens.append("extern");
354 tokens.append("crate");
355 if let Some(ref original) = *original {
356 original.to_tokens(tokens);
357 tokens.append("as");
358 }
359 self.ident.to_tokens(tokens);
360 tokens.append(";");
361 }
362 ItemKind::Use(ref _view_path) => unimplemented!(),
David Tolnay47a877c2016-10-01 16:50:55 -0700363 ItemKind::Static(ref ty, ref mutability, ref expr) => {
364 self.vis.to_tokens(tokens);
365 tokens.append("static");
366 mutability.to_tokens(tokens);
367 self.ident.to_tokens(tokens);
368 tokens.append(":");
369 ty.to_tokens(tokens);
370 tokens.append("=");
371 expr.to_tokens(tokens);
372 tokens.append(";");
373 }
374 ItemKind::Const(ref ty, ref expr) => {
375 self.vis.to_tokens(tokens);
376 tokens.append("const");
377 self.ident.to_tokens(tokens);
378 tokens.append(":");
379 ty.to_tokens(tokens);
380 tokens.append("=");
381 expr.to_tokens(tokens);
382 tokens.append(";");
383 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700384 ItemKind::Fn(ref _decl, _unsafety, _constness, ref _abi, ref _generics, ref _block) => unimplemented!(),
385 ItemKind::Mod(ref _items) => unimplemented!(),
386 ItemKind::ForeignMod(ref _foreign_mod) => unimplemented!(),
David Tolnay3cf52982016-10-01 17:11:37 -0700387 ItemKind::Ty(ref ty, ref generics) => {
388 self.vis.to_tokens(tokens);
389 tokens.append("type");
390 self.ident.to_tokens(tokens);
391 generics.to_tokens(tokens);
392 tokens.append("=");
393 ty.to_tokens(tokens);
394 tokens.append(";");
395 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700396 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700397 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700398 tokens.append("enum");
399 self.ident.to_tokens(tokens);
400 generics.to_tokens(tokens);
401 generics.where_clause.to_tokens(tokens);
402 tokens.append("{");
403 for variant in variants {
404 variant.to_tokens(tokens);
405 tokens.append(",");
406 }
407 tokens.append("}");
408 }
409 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700410 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700411 tokens.append("struct");
412 self.ident.to_tokens(tokens);
413 generics.to_tokens(tokens);
414 generics.where_clause.to_tokens(tokens);
415 variant_data.to_tokens(tokens);
416 match *variant_data {
417 VariantData::Struct(_) => { /* no semicolon */ }
418 VariantData::Tuple(_) |
419 VariantData::Unit => tokens.append(";"),
420 }
421 }
422 ItemKind::Union(ref _variant_data, ref _generics) => unimplemented!(),
423 ItemKind::Trait(_unsafety, ref _generics, ref _bound, ref _item) => unimplemented!(),
424 ItemKind::DefaultImpl(_unsafety, ref _path) => unimplemented!(),
425 ItemKind::Impl(_unsafety, _polarity, ref _generics, ref _path, ref _ty, ref _item) => unimplemented!(),
426 ItemKind::Mac(ref _mac) => unimplemented!(),
427 }
428 }
429 }
430}