blob: 011a6fff784fa864daea00188d501bf6d8bdcaa9 [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 Tolnayedf2b992016-09-23 20:43:45 -0700215 use ident::parsing::ident;
216 use macro_input::{Body, MacroInput};
217 use macro_input::parsing::macro_input;
David Tolnay47a877c2016-10-01 16:50:55 -0700218 use ty::parsing::{mutability, ty};
David Tolnayedf2b992016-09-23 20:43:45 -0700219
220 named!(pub item -> Item, alt!(
David Tolnaya96a3fa2016-09-24 07:17:42 -0700221 item_extern_crate
222 // TODO: Use
David Tolnay47a877c2016-10-01 16:50:55 -0700223 |
224 item_static
225 |
226 item_const
David Tolnaya96a3fa2016-09-24 07:17:42 -0700227 // TODO: Fn
228 // TODO: Mod
229 // TODO: ForeignMod
230 // TODO: Ty
David Tolnayedf2b992016-09-23 20:43:45 -0700231 |
David Tolnaya96a3fa2016-09-24 07:17:42 -0700232 item_struct_or_enum
233 // TODO: Union
234 // TODO: Trait
235 // TODO: DefaultImpl
236 // TODO: Impl
237 // TODO: Mac
David Tolnayedf2b992016-09-23 20:43:45 -0700238 ));
239
David Tolnaya96a3fa2016-09-24 07:17:42 -0700240 named!(item_extern_crate -> Item, do_parse!(
David Tolnay4a51dc72016-10-01 00:40:31 -0700241 attrs: many0!(outer_attr) >>
David Tolnayedf2b992016-09-23 20:43:45 -0700242 vis: visibility >>
David Tolnay10413f02016-09-30 09:12:02 -0700243 keyword!("extern") >>
244 keyword!("crate") >>
David Tolnayedf2b992016-09-23 20:43:45 -0700245 id: ident >>
246 rename: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700247 keyword!("as"),
David Tolnayedf2b992016-09-23 20:43:45 -0700248 ident
249 )) >>
250 punct!(";") >>
251 ({
252 let (name, original_name) = match rename {
253 Some(rename) => (rename, Some(id)),
254 None => (id, None),
255 };
256 Item {
257 ident: name,
258 vis: vis,
259 attrs: attrs,
260 node: ItemKind::ExternCrate(original_name),
261 }
262 })
263 ));
264
David Tolnay47a877c2016-10-01 16:50:55 -0700265 named!(item_static -> Item, do_parse!(
266 attrs: many0!(outer_attr) >>
267 vis: visibility >>
268 keyword!("static") >>
269 mutability: mutability >>
270 id: ident >>
271 punct!(":") >>
272 ty: ty >>
273 punct!("=") >>
274 value: expr >>
275 punct!(";") >>
276 (Item {
277 ident: id,
278 vis: vis,
279 attrs: attrs,
280 node: ItemKind::Static(Box::new(ty), mutability, Box::new(value)),
281 })
282 ));
283
284 named!(item_const -> Item, do_parse!(
285 attrs: many0!(outer_attr) >>
286 vis: visibility >>
287 keyword!("const") >>
288 id: ident >>
289 punct!(":") >>
290 ty: ty >>
291 punct!("=") >>
292 value: expr >>
293 punct!(";") >>
294 (Item {
295 ident: id,
296 vis: vis,
297 attrs: attrs,
298 node: ItemKind::Const(Box::new(ty), Box::new(value)),
299 })
300 ));
301
David Tolnaya96a3fa2016-09-24 07:17:42 -0700302 named!(item_struct_or_enum -> Item, map!(
David Tolnayedf2b992016-09-23 20:43:45 -0700303 macro_input,
304 |def: MacroInput| Item {
305 ident: def.ident,
306 vis: def.vis,
307 attrs: def.attrs,
308 node: match def.body {
309 Body::Enum(variants) => {
310 ItemKind::Enum(variants, def.generics)
311 }
312 Body::Struct(variant_data) => {
313 ItemKind::Struct(variant_data, def.generics)
314 }
315 }
316 }
317 ));
318}
David Tolnay4a51dc72016-10-01 00:40:31 -0700319
320#[cfg(feature = "printing")]
321mod printing {
322 use super::*;
323 use attr::FilterAttrs;
David Tolnay47a877c2016-10-01 16:50:55 -0700324 use data::VariantData;
David Tolnay4a51dc72016-10-01 00:40:31 -0700325 use quote::{Tokens, ToTokens};
326
327 impl ToTokens for Item {
328 fn to_tokens(&self, tokens: &mut Tokens) {
329 for attr in self.attrs.outer() {
330 attr.to_tokens(tokens);
331 }
332 match self.node {
333 ItemKind::ExternCrate(ref original) => {
334 tokens.append("extern");
335 tokens.append("crate");
336 if let Some(ref original) = *original {
337 original.to_tokens(tokens);
338 tokens.append("as");
339 }
340 self.ident.to_tokens(tokens);
341 tokens.append(";");
342 }
343 ItemKind::Use(ref _view_path) => unimplemented!(),
David Tolnay47a877c2016-10-01 16:50:55 -0700344 ItemKind::Static(ref ty, ref mutability, ref expr) => {
345 self.vis.to_tokens(tokens);
346 tokens.append("static");
347 mutability.to_tokens(tokens);
348 self.ident.to_tokens(tokens);
349 tokens.append(":");
350 ty.to_tokens(tokens);
351 tokens.append("=");
352 expr.to_tokens(tokens);
353 tokens.append(";");
354 }
355 ItemKind::Const(ref ty, ref expr) => {
356 self.vis.to_tokens(tokens);
357 tokens.append("const");
358 self.ident.to_tokens(tokens);
359 tokens.append(":");
360 ty.to_tokens(tokens);
361 tokens.append("=");
362 expr.to_tokens(tokens);
363 tokens.append(";");
364 }
David Tolnay4a51dc72016-10-01 00:40:31 -0700365 ItemKind::Fn(ref _decl, _unsafety, _constness, ref _abi, ref _generics, ref _block) => unimplemented!(),
366 ItemKind::Mod(ref _items) => unimplemented!(),
367 ItemKind::ForeignMod(ref _foreign_mod) => unimplemented!(),
368 ItemKind::Ty(ref _ty, ref _generics) => unimplemented!(),
369 ItemKind::Enum(ref variants, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700370 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700371 tokens.append("enum");
372 self.ident.to_tokens(tokens);
373 generics.to_tokens(tokens);
374 generics.where_clause.to_tokens(tokens);
375 tokens.append("{");
376 for variant in variants {
377 variant.to_tokens(tokens);
378 tokens.append(",");
379 }
380 tokens.append("}");
381 }
382 ItemKind::Struct(ref variant_data, ref generics) => {
David Tolnay47a877c2016-10-01 16:50:55 -0700383 self.vis.to_tokens(tokens);
David Tolnay4a51dc72016-10-01 00:40:31 -0700384 tokens.append("struct");
385 self.ident.to_tokens(tokens);
386 generics.to_tokens(tokens);
387 generics.where_clause.to_tokens(tokens);
388 variant_data.to_tokens(tokens);
389 match *variant_data {
390 VariantData::Struct(_) => { /* no semicolon */ }
391 VariantData::Tuple(_) |
392 VariantData::Unit => tokens.append(";"),
393 }
394 }
395 ItemKind::Union(ref _variant_data, ref _generics) => unimplemented!(),
396 ItemKind::Trait(_unsafety, ref _generics, ref _bound, ref _item) => unimplemented!(),
397 ItemKind::DefaultImpl(_unsafety, ref _path) => unimplemented!(),
398 ItemKind::Impl(_unsafety, _polarity, ref _generics, ref _path, ref _ty, ref _item) => unimplemented!(),
399 ItemKind::Mac(ref _mac) => unimplemented!(),
400 }
401 }
402 }
403}