David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 3 | /// An item |
| 4 | /// |
| 5 | /// The name might be a dummy name in case of anonymous items |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 6 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 7 | pub struct Item { |
| 8 | pub ident: Ident, |
| 9 | pub vis: Visibility, |
| 10 | pub attrs: Vec<Attribute>, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 11 | pub node: ItemKind, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 15 | pub 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 Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 84 | pub 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 Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 90 | Simple(Ident, Path), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 91 | |
| 92 | /// `foo::bar::*` |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 93 | Glob(Path), |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 94 | |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 95 | /// `foo::bar::{a, b, c}` |
| 96 | List(Path, Vec<PathListItem>) |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 100 | pub 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)] |
| 107 | pub enum Unsafety { |
| 108 | Unsafe, |
| 109 | Normal, |
| 110 | } |
| 111 | |
| 112 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 113 | pub enum Constness { |
| 114 | Const, |
| 115 | NotConst, |
| 116 | } |
| 117 | |
| 118 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 119 | pub enum Defaultness { |
| 120 | Default, |
| 121 | Final, |
| 122 | } |
| 123 | |
| 124 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 125 | pub struct Abi(pub String); |
| 126 | |
| 127 | /// Foreign module declaration. |
| 128 | /// |
| 129 | /// E.g. `extern { .. }` or `extern C { .. }` |
| 130 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 131 | pub struct ForeignMod { |
| 132 | pub abi: Abi, |
| 133 | pub items: Vec<ForeignItem>, |
| 134 | } |
| 135 | |
| 136 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 137 | pub struct ForeignItem { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 138 | pub ident: Ident, |
| 139 | pub attrs: Vec<Attribute>, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 140 | pub node: ForeignItemKind, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 141 | pub vis: Visibility, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 144 | /// An item within an `extern` block |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 145 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 146 | pub 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)] |
| 159 | pub struct TraitItem { |
| 160 | pub ident: Ident, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 161 | pub attrs: Vec<Attribute>, |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 162 | pub node: TraitItemKind, |
| 163 | } |
| 164 | |
| 165 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 166 | pub enum TraitItemKind { |
| 167 | Const(Ty, Option<Expr>), |
| 168 | Method(MethodSig, Option<Block>), |
| 169 | Type(Vec<TyParamBound>, Option<Ty>), |
| 170 | Macro(Mac), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 173 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 174 | pub enum ImplPolarity { |
| 175 | /// `impl Trait for Type` |
| 176 | Positive, |
| 177 | /// `impl !Trait for Type` |
| 178 | Negative, |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 179 | } |
| 180 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 181 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 182 | pub struct ImplItem { |
| 183 | pub ident: Ident, |
| 184 | pub vis: Visibility, |
| 185 | pub defaultness: Defaultness, |
| 186 | pub attrs: Vec<Attribute>, |
| 187 | pub node: ImplItemKind, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 190 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 191 | pub enum ImplItemKind { |
| 192 | Const(Ty, Expr), |
| 193 | Method(MethodSig, Block), |
| 194 | Type(Ty), |
| 195 | Macro(Mac), |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 196 | } |
David Tolnay | d502581 | 2016-09-04 14:21:46 -0700 | [diff] [blame] | 197 | |
David Tolnay | f38cdf6 | 2016-09-23 19:07:09 -0700 | [diff] [blame] | 198 | /// Represents a method's signature in a trait declaration, |
| 199 | /// or in an implementation. |
| 200 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 201 | pub struct MethodSig { |
| 202 | pub unsafety: Unsafety, |
| 203 | pub constness: Constness, |
| 204 | pub abi: Abi, |
| 205 | pub decl: FnDecl, |
| 206 | pub generics: Generics, |
David Tolnay | d502581 | 2016-09-04 14:21:46 -0700 | [diff] [blame] | 207 | } |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 208 | |
| 209 | #[cfg(feature = "parsing")] |
| 210 | pub mod parsing { |
| 211 | use super::*; |
| 212 | use attr::parsing::attribute; |
| 213 | use data::parsing::visibility; |
| 214 | use ident::parsing::ident; |
| 215 | use macro_input::{Body, MacroInput}; |
| 216 | use macro_input::parsing::macro_input; |
| 217 | use nom::multispace; |
| 218 | |
| 219 | named!(pub item -> Item, alt!( |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame^] | 220 | item_extern_crate |
| 221 | // TODO: Use |
| 222 | // TODO: Static |
| 223 | // TODO: Const |
| 224 | // TODO: Fn |
| 225 | // TODO: Mod |
| 226 | // TODO: ForeignMod |
| 227 | // TODO: Ty |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 228 | | |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame^] | 229 | item_struct_or_enum |
| 230 | // TODO: Union |
| 231 | // TODO: Trait |
| 232 | // TODO: DefaultImpl |
| 233 | // TODO: Impl |
| 234 | // TODO: Mac |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 235 | )); |
| 236 | |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame^] | 237 | named!(item_extern_crate -> Item, do_parse!( |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 238 | attrs: many0!(attribute) >> |
| 239 | vis: visibility >> |
| 240 | punct!("extern") >> |
| 241 | multispace >> |
| 242 | punct!("crate") >> |
| 243 | multispace >> |
| 244 | id: ident >> |
| 245 | rename: option!(preceded!( |
| 246 | tuple!(punct!("as"), multispace), |
| 247 | ident |
| 248 | )) >> |
| 249 | punct!(";") >> |
| 250 | ({ |
| 251 | let (name, original_name) = match rename { |
| 252 | Some(rename) => (rename, Some(id)), |
| 253 | None => (id, None), |
| 254 | }; |
| 255 | Item { |
| 256 | ident: name, |
| 257 | vis: vis, |
| 258 | attrs: attrs, |
| 259 | node: ItemKind::ExternCrate(original_name), |
| 260 | } |
| 261 | }) |
| 262 | )); |
| 263 | |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame^] | 264 | named!(item_struct_or_enum -> Item, map!( |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 265 | macro_input, |
| 266 | |def: MacroInput| Item { |
| 267 | ident: def.ident, |
| 268 | vis: def.vis, |
| 269 | attrs: def.attrs, |
| 270 | node: match def.body { |
| 271 | Body::Enum(variants) => { |
| 272 | ItemKind::Enum(variants, def.generics) |
| 273 | } |
| 274 | Body::Struct(variant_data) => { |
| 275 | ItemKind::Struct(variant_data, def.generics) |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | )); |
| 280 | } |