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::*; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 212 | use attr::parsing::outer_attr; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 213 | use data::parsing::visibility; |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 214 | use expr::parsing::expr; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 215 | use ident::parsing::ident; |
| 216 | use macro_input::{Body, MacroInput}; |
| 217 | use macro_input::parsing::macro_input; |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 218 | use ty::parsing::{mutability, ty}; |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 219 | |
| 220 | named!(pub item -> Item, alt!( |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 221 | item_extern_crate |
| 222 | // TODO: Use |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 223 | | |
| 224 | item_static |
| 225 | | |
| 226 | item_const |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 227 | // TODO: Fn |
| 228 | // TODO: Mod |
| 229 | // TODO: ForeignMod |
| 230 | // TODO: Ty |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 231 | | |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 232 | item_struct_or_enum |
| 233 | // TODO: Union |
| 234 | // TODO: Trait |
| 235 | // TODO: DefaultImpl |
| 236 | // TODO: Impl |
| 237 | // TODO: Mac |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 238 | )); |
| 239 | |
David Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 240 | named!(item_extern_crate -> Item, do_parse!( |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 241 | attrs: many0!(outer_attr) >> |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 242 | vis: visibility >> |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 243 | keyword!("extern") >> |
| 244 | keyword!("crate") >> |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 245 | id: ident >> |
| 246 | rename: option!(preceded!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 247 | keyword!("as"), |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 248 | 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 Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 265 | 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 Tolnay | a96a3fa | 2016-09-24 07:17:42 -0700 | [diff] [blame] | 302 | named!(item_struct_or_enum -> Item, map!( |
David Tolnay | edf2b99 | 2016-09-23 20:43:45 -0700 | [diff] [blame] | 303 | 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 Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 319 | |
| 320 | #[cfg(feature = "printing")] |
| 321 | mod printing { |
| 322 | use super::*; |
| 323 | use attr::FilterAttrs; |
David Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 324 | use data::VariantData; |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 325 | 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 Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 344 | 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 Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 365 | 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 Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 370 | self.vis.to_tokens(tokens); |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 371 | 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 Tolnay | 47a877c | 2016-10-01 16:50:55 -0700 | [diff] [blame^] | 383 | self.vis.to_tokens(tokens); |
David Tolnay | 4a51dc7 | 2016-10-01 00:40:31 -0700 | [diff] [blame] | 384 | 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 | } |