2013-09-12 20:10:51 -05:00
|
|
|
// 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 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
//! This module is used to store stuff from Rust's AST in a more convenient
|
|
|
|
//! manner (and with prettier names) before cleaning.
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::StructType::*;
|
|
|
|
pub use self::TypeBound::*;
|
2013-08-15 15:28:54 -05:00
|
|
|
|
|
|
|
use syntax;
|
2013-09-05 09:14:35 -05:00
|
|
|
use syntax::codemap::Span;
|
2015-04-07 16:22:55 -05:00
|
|
|
use syntax::abi;
|
2013-08-15 15:28:54 -05:00
|
|
|
use syntax::ast;
|
2015-09-20 08:47:24 -05:00
|
|
|
use syntax::ast::{Name, NodeId};
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::attr;
|
2014-05-18 08:56:13 -05:00
|
|
|
use syntax::ptr::P;
|
2015-07-31 02:04:06 -05:00
|
|
|
use rustc_front::hir;
|
2014-05-16 12:15:33 -05:00
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
pub struct Module {
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Option<Name>,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-04-26 15:08:36 -05:00
|
|
|
pub where_outer: Span,
|
|
|
|
pub where_inner: Span,
|
2014-12-26 02:55:16 -06:00
|
|
|
pub extern_crates: Vec<ExternCrate>,
|
|
|
|
pub imports: Vec<Import>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub structs: Vec<Struct>,
|
|
|
|
pub enums: Vec<Enum>,
|
|
|
|
pub fns: Vec<Function>,
|
|
|
|
pub mods: Vec<Module>,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub typedefs: Vec<Typedef>,
|
|
|
|
pub statics: Vec<Static>,
|
2014-10-06 19:41:15 -05:00
|
|
|
pub constants: Vec<Constant>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub traits: Vec<Trait>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub impls: Vec<Impl>,
|
2015-02-07 07:24:34 -06:00
|
|
|
pub def_traits: Vec<DefaultImpl>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub foreigns: Vec<hir::ForeignMod>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub macros: Vec<Macro>,
|
|
|
|
pub is_crate: bool,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
2015-09-19 20:50:30 -05:00
|
|
|
pub fn new(name: Option<Name>) -> Module {
|
2013-08-15 15:28:54 -05:00
|
|
|
Module {
|
|
|
|
name : name,
|
|
|
|
id: 0,
|
2015-07-31 02:04:06 -05:00
|
|
|
vis: hir::Inherited,
|
2014-06-26 13:37:39 -05:00
|
|
|
stab: None,
|
2014-04-26 15:08:36 -05:00
|
|
|
where_outer: syntax::codemap::DUMMY_SP,
|
|
|
|
where_inner: syntax::codemap::DUMMY_SP,
|
2014-03-05 17:28:08 -06:00
|
|
|
attrs : Vec::new(),
|
2014-12-26 02:55:16 -06:00
|
|
|
extern_crates: Vec::new(),
|
|
|
|
imports : Vec::new(),
|
2014-03-05 17:28:08 -06:00
|
|
|
structs : Vec::new(),
|
|
|
|
enums : Vec::new(),
|
|
|
|
fns : Vec::new(),
|
|
|
|
mods : Vec::new(),
|
|
|
|
typedefs : Vec::new(),
|
|
|
|
statics : Vec::new(),
|
2014-10-06 19:41:15 -05:00
|
|
|
constants : Vec::new(),
|
2014-03-05 17:28:08 -06:00
|
|
|
traits : Vec::new(),
|
|
|
|
impls : Vec::new(),
|
2015-01-26 17:35:03 -06:00
|
|
|
def_traits : Vec::new(),
|
2014-03-05 17:28:08 -06:00
|
|
|
foreigns : Vec::new(),
|
|
|
|
macros : Vec::new(),
|
2014-02-28 15:33:45 -06:00
|
|
|
is_crate : false,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
|
2013-08-15 15:28:54 -05:00
|
|
|
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,
|
2015-07-31 02:04:06 -05:00
|
|
|
TraitBound(hir::TraitRef)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Struct {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: NodeId,
|
|
|
|
pub struct_type: StructType,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub generics: hir::Generics,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fields: Vec<hir::StructField>,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub variants: Vec<Variant>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub generics: hir::Generics,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: NodeId,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
2015-09-20 08:47:24 -05:00
|
|
|
pub name: Name,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-10-01 16:03:22 -05:00
|
|
|
pub def: P<hir::StructDef>,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub decl: hir::FnDecl,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: NodeId,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub unsafety: hir::Unsafety,
|
|
|
|
pub constness: hir::Constness,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub generics: hir::Generics,
|
2015-04-07 16:22:55 -05:00
|
|
|
pub abi: abi::Abi,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Typedef {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub ty: P<hir::Ty>,
|
|
|
|
pub gen: hir::Generics,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: ast::NodeId,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Debug)]
|
2013-08-15 15:28:54 -05:00
|
|
|
pub struct Static {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub type_: P<hir::Ty>,
|
|
|
|
pub mutability: hir::Mutability,
|
|
|
|
pub expr: P<hir::Expr>,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: ast::NodeId,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2014-10-06 19:41:15 -05:00
|
|
|
pub struct Constant {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub type_: P<hir::Ty>,
|
|
|
|
pub expr: P<hir::Expr>,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-10-06 19:41:15 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
pub struct Trait {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub unsafety: hir::Unsafety,
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub items: Vec<P<hir::TraitItem>>, //should be TraitItem
|
|
|
|
pub generics: hir::Generics,
|
|
|
|
pub bounds: Vec<hir::TyParamBound>,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: ast::NodeId,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub unsafety: hir::Unsafety,
|
|
|
|
pub polarity: hir::ImplPolarity,
|
|
|
|
pub generics: hir::Generics,
|
|
|
|
pub trait_: Option<hir::TraitRef>,
|
|
|
|
pub for_: P<hir::Ty>,
|
|
|
|
pub items: Vec<P<hir::ImplItem>>,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: ast::NodeId,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2015-02-07 07:24:34 -06:00
|
|
|
pub struct DefaultImpl {
|
2015-07-31 02:04:06 -05:00
|
|
|
pub unsafety: hir::Unsafety,
|
|
|
|
pub trait_: hir::TraitRef,
|
2015-01-26 17:35:03 -06:00
|
|
|
pub id: ast::NodeId,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-03-12 21:15:52 -05:00
|
|
|
pub whence: Span,
|
2015-01-26 17:35:03 -06:00
|
|
|
}
|
|
|
|
|
2014-02-16 23:40:26 -06:00
|
|
|
pub struct Macro {
|
2015-09-20 06:51:40 -05:00
|
|
|
pub name: Name,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub id: ast::NodeId,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 11:32:26 -05:00
|
|
|
pub whence: Span,
|
2014-06-26 13:37:39 -05:00
|
|
|
pub stab: Option<attr::Stability>,
|
2015-09-20 06:51:40 -05:00
|
|
|
pub imported_from: Option<Name>,
|
2014-02-16 23:40:26 -06:00
|
|
|
}
|
|
|
|
|
2014-12-26 02:55:16 -06:00
|
|
|
pub struct ExternCrate {
|
2015-09-19 20:50:30 -05:00
|
|
|
pub name: Name,
|
2014-12-26 02:55:16 -06:00
|
|
|
pub path: Option<String>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-12-26 02:55:16 -06:00
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Import {
|
|
|
|
pub id: NodeId,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub vis: hir::Visibility,
|
2015-09-14 04:58:20 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2015-07-31 02:04:06 -05:00
|
|
|
pub node: hir::ViewPath_,
|
2014-12-26 02:55:16 -06:00
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn struct_type_from_def(sd: &hir::StructDef) -> StructType {
|
2015-10-01 19:53:28 -05:00
|
|
|
if sd.kind != hir::VariantKind::Dict {
|
2013-08-15 15:28:54 -05:00
|
|
|
// We are in a tuple-struct
|
|
|
|
match sd.fields.len() {
|
|
|
|
0 => Unit,
|
|
|
|
1 => Newtype,
|
|
|
|
_ => Tuple
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Plain
|
|
|
|
}
|
|
|
|
}
|