// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! This module is used to store stuff from Rust's AST in a more convenient //! manner (and with prettier names) before cleaning. pub use self::StructType::*; pub use self::TypeBound::*; use syntax; use syntax::codemap::Span; use syntax::ast; use syntax::attr; use syntax::ast::{Ident, NodeId}; use syntax::ptr::P; pub struct Module { pub name: Option, pub attrs: Vec, pub where_outer: Span, pub where_inner: Span, pub extern_crates: Vec, pub imports: Vec, pub structs: Vec, pub enums: Vec, pub fns: Vec, pub mods: Vec, pub id: NodeId, pub typedefs: Vec, pub statics: Vec, pub constants: Vec, pub traits: Vec, pub vis: ast::Visibility, pub stab: Option, pub impls: Vec, pub def_traits: Vec, pub foreigns: Vec, pub macros: Vec, pub is_crate: bool, } impl Module { pub fn new(name: Option) -> Module { Module { name : name, id: 0, vis: ast::Inherited, stab: None, where_outer: syntax::codemap::DUMMY_SP, where_inner: syntax::codemap::DUMMY_SP, attrs : Vec::new(), extern_crates: Vec::new(), imports : Vec::new(), structs : Vec::new(), enums : Vec::new(), fns : Vec::new(), mods : Vec::new(), typedefs : Vec::new(), statics : Vec::new(), constants : Vec::new(), traits : Vec::new(), impls : Vec::new(), def_traits : Vec::new(), foreigns : Vec::new(), macros : Vec::new(), is_crate : false, } } } #[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)] pub enum StructType { /// A normal struct Plain, /// A tuple struct Tuple, /// A newtype struct (tuple struct with one element) Newtype, /// A unit struct Unit } pub enum TypeBound { RegionBound, TraitBound(ast::TraitRef) } pub struct Struct { pub vis: ast::Visibility, pub stab: Option, pub id: NodeId, pub struct_type: StructType, pub name: Ident, pub generics: ast::Generics, pub attrs: Vec, pub fields: Vec, pub whence: Span, } pub struct Enum { pub vis: ast::Visibility, pub stab: Option, pub variants: Vec, pub generics: ast::Generics, pub attrs: Vec, pub id: NodeId, pub whence: Span, pub name: Ident, } pub struct Variant { pub name: Ident, pub attrs: Vec, pub kind: ast::VariantKind, pub id: ast::NodeId, pub vis: ast::Visibility, pub stab: Option, pub whence: Span, } pub struct Function { pub decl: ast::FnDecl, pub attrs: Vec, pub id: NodeId, pub name: Ident, pub vis: ast::Visibility, pub stab: Option, pub unsafety: ast::Unsafety, pub whence: Span, pub generics: ast::Generics, } pub struct Typedef { pub ty: P, pub gen: ast::Generics, pub name: Ident, pub id: ast::NodeId, pub attrs: Vec, pub whence: Span, pub vis: ast::Visibility, pub stab: Option, } #[derive(Debug)] pub struct Static { pub type_: P, pub mutability: ast::Mutability, pub expr: P, pub name: Ident, pub attrs: Vec, pub vis: ast::Visibility, pub stab: Option, pub id: ast::NodeId, pub whence: Span, } pub struct Constant { pub type_: P, pub expr: P, pub name: Ident, pub attrs: Vec, pub vis: ast::Visibility, pub stab: Option, pub id: ast::NodeId, pub whence: Span, } pub struct Trait { pub unsafety: ast::Unsafety, pub name: Ident, pub items: Vec>, //should be TraitItem pub generics: ast::Generics, pub bounds: Vec, pub attrs: Vec, pub id: ast::NodeId, pub whence: Span, pub vis: ast::Visibility, pub stab: Option, } pub struct Impl { pub unsafety: ast::Unsafety, pub polarity: ast::ImplPolarity, pub generics: ast::Generics, pub trait_: Option, pub for_: P, pub items: Vec>, pub attrs: Vec, pub whence: Span, pub vis: ast::Visibility, pub stab: Option, pub id: ast::NodeId, } pub struct DefaultImpl { pub unsafety: ast::Unsafety, pub trait_: ast::TraitRef, pub id: ast::NodeId, pub attrs: Vec, pub whence: Span, } pub struct Macro { pub name: Ident, pub id: ast::NodeId, pub attrs: Vec, pub whence: Span, pub stab: Option, } pub struct ExternCrate { pub name: Ident, pub path: Option, pub vis: ast::Visibility, pub attrs: Vec, pub whence: Span, } pub struct Import { pub id: NodeId, pub vis: ast::Visibility, pub attrs: Vec, pub node: ast::ViewPath_, pub whence: Span, } pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType { if sd.ctor_id.is_some() { // We are in a tuple-struct match sd.fields.len() { 0 => Unit, 1 => Newtype, _ => Tuple } } else { Plain } }