blob: 95af98d6f12979c5e90a84b8c046ebc253b4ddbd [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnayb79ee962016-09-04 09:39:20 -07003#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct Item {
5 pub ident: Ident,
6 pub vis: Visibility,
7 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -07008 pub node: ItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -07009}
10
11#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070012pub enum ItemKind {
13 /// An`extern crate` item, with optional original crate name.
14 ///
15 /// E.g. `extern crate foo` or `extern crate foo_bar as foo`
16 ExternCrate(Option<Ident>),
17 /// A use declaration (`use` or `pub use`) item.
18 ///
19 /// E.g. `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`
20 Use(Box<ViewPath>),
21 /// A static item (`static` or `pub static`).
22 ///
23 /// E.g. `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`
24 Static(Box<Ty>, Mutability, Box<Expr>),
25 /// A constant item (`const` or `pub const`).
26 ///
27 /// E.g. `const FOO: i32 = 42;`
28 Const(Box<Ty>, Box<Expr>),
29 /// A function declaration (`fn` or `pub fn`).
30 ///
31 /// E.g. `fn foo(bar: usize) -> usize { .. }`
32 Fn(Box<FnDecl>, Unsafety, Constness, Abi, Generics, Box<Block>),
33 /// A module declaration (`mod` or `pub mod`).
34 ///
35 /// E.g. `mod foo;` or `mod foo { .. }`
36 Mod(Vec<Item>),
37 /// An external module (`extern` or `pub extern`).
38 ///
39 /// E.g. `extern {}` or `extern "C" {}`
40 ForeignMod(ForeignMod),
41 /// A type alias (`type` or `pub type`).
42 ///
43 /// E.g. `type Foo = Bar<u8>;`
44 Ty(Box<Ty>, Generics),
45 /// An enum definition (`enum` or `pub enum`).
46 ///
47 /// E.g. `enum Foo<A, B> { C<A>, D<B> }`
48 Enum(Vec<Variant>, Generics),
49 /// A struct definition (`struct` or `pub struct`).
50 ///
51 /// E.g. `struct Foo<A> { x: A }`
52 Struct(VariantData, Generics),
53 /// A union definition (`union` or `pub union`).
54 ///
55 /// E.g. `union Foo<A, B> { x: A, y: B }`
56 Union(VariantData, Generics),
57 /// A Trait declaration (`trait` or `pub trait`).
58 ///
59 /// E.g. `trait Foo { .. }` or `trait Foo<T> { .. }`
60 Trait(Unsafety, Generics, Vec<TyParamBound>, Vec<TraitItem>),
61 // Default trait implementation.
62 ///
63 /// E.g. `impl Trait for .. {}` or `impl<T> Trait<T> for .. {}`
64 DefaultImpl(Unsafety, Path),
65 /// An implementation.
66 ///
67 /// E.g. `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`
68 Impl(Unsafety,
69 ImplPolarity,
70 Generics,
71 Option<Path>, // (optional) trait this impl implements
72 Box<Ty>, // self
73 Vec<ImplItem>),
74 /// A macro invocation (which includes macro definition).
75 ///
76 /// E.g. `macro_rules! foo { .. }` or `foo!(..)`
77 Mac(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -070078}
79
80#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -070081pub enum ViewPath {
82 /// `foo::bar::baz as quux`
83 ///
84 /// or just
85 ///
86 /// `foo::bar::baz` (with `as baz` implicitly on the right)
87 ViewPathSimple(Ident, Path),
88
89 /// `foo::bar::*`
90 ViewPathGlob(Path),
91
92 /// `foo::bar::{a,b,c}`
93 ViewPathList(Path, Vec<PathListItem>)
94}
95
96#[derive(Debug, Clone, Eq, PartialEq)]
97pub struct PathListItem {
98 pub name: Ident,
99 /// renamed in list, e.g. `use foo::{bar as baz};`
100 pub rename: Option<Ident>,
101}
102
103#[derive(Debug, Copy, Clone, Eq, PartialEq)]
104pub enum Unsafety {
105 Unsafe,
106 Normal,
107}
108
109#[derive(Debug, Copy, Clone, Eq, PartialEq)]
110pub enum Constness {
111 Const,
112 NotConst,
113}
114
115#[derive(Debug, Copy, Clone, Eq, PartialEq)]
116pub enum Defaultness {
117 Default,
118 Final,
119}
120
121#[derive(Debug, Clone, Eq, PartialEq)]
122pub struct Abi(pub String);
123
124/// Foreign module declaration.
125///
126/// E.g. `extern { .. }` or `extern C { .. }`
127#[derive(Debug, Clone, Eq, PartialEq)]
128pub struct ForeignMod {
129 pub abi: Abi,
130 pub items: Vec<ForeignItem>,
131}
132
133#[derive(Debug, Clone, Eq, PartialEq)]
134pub struct ForeignItem {
David Tolnayb79ee962016-09-04 09:39:20 -0700135 pub ident: Ident,
136 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700137 pub node: ForeignItemKind,
David Tolnayb79ee962016-09-04 09:39:20 -0700138 pub vis: Visibility,
David Tolnayf38cdf62016-09-23 19:07:09 -0700139}
140
141#[derive(Debug, Clone, Eq, PartialEq)]
142pub enum ForeignItemKind {
143 /// A foreign function
144 Fn(Box<FnDecl>, Generics),
145 /// A foreign static item (`static ext: u8`), with optional mutability
146 /// (the boolean is true when mutable)
147 Static(Box<Ty>, bool),
148}
149
150/// Represents an item declaration within a trait declaration,
151/// possibly including a default implementation. A trait item is
152/// either required (meaning it doesn't have an implementation, just a
153/// signature) or provided (meaning it has a default implementation).
154#[derive(Debug, Clone, Eq, PartialEq)]
155pub struct TraitItem {
156 pub ident: Ident,
David Tolnayb79ee962016-09-04 09:39:20 -0700157 pub attrs: Vec<Attribute>,
David Tolnayf38cdf62016-09-23 19:07:09 -0700158 pub node: TraitItemKind,
159}
160
161#[derive(Debug, Clone, Eq, PartialEq)]
162pub enum TraitItemKind {
163 Const(Ty, Option<Expr>),
164 Method(MethodSig, Option<Block>),
165 Type(Vec<TyParamBound>, Option<Ty>),
166 Macro(Mac),
David Tolnayb79ee962016-09-04 09:39:20 -0700167}
168
David Tolnay55337722016-09-11 12:58:56 -0700169#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayf38cdf62016-09-23 19:07:09 -0700170pub enum ImplPolarity {
171 /// `impl Trait for Type`
172 Positive,
173 /// `impl !Trait for Type`
174 Negative,
David Tolnay55337722016-09-11 12:58:56 -0700175}
176
David Tolnayf38cdf62016-09-23 19:07:09 -0700177#[derive(Debug, Clone, Eq, PartialEq)]
178pub struct ImplItem {
179 pub ident: Ident,
180 pub vis: Visibility,
181 pub defaultness: Defaultness,
182 pub attrs: Vec<Attribute>,
183 pub node: ImplItemKind,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700184}
185
David Tolnayf38cdf62016-09-23 19:07:09 -0700186#[derive(Debug, Clone, Eq, PartialEq)]
187pub enum ImplItemKind {
188 Const(Ty, Expr),
189 Method(MethodSig, Block),
190 Type(Ty),
191 Macro(Mac),
David Tolnay9d8f1972016-09-04 11:58:48 -0700192}
David Tolnayd5025812016-09-04 14:21:46 -0700193
David Tolnayf38cdf62016-09-23 19:07:09 -0700194/// Represents a method's signature in a trait declaration,
195/// or in an implementation.
196#[derive(Debug, Clone, Eq, PartialEq)]
197pub struct MethodSig {
198 pub unsafety: Unsafety,
199 pub constness: Constness,
200 pub abi: Abi,
201 pub decl: FnDecl,
202 pub generics: Generics,
David Tolnayd5025812016-09-04 14:21:46 -0700203}