2013-09-12 21:10:51 -04: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 16:28:54 -04: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 00:05:53 -08:00
|
|
|
pub use self::StructType::*;
|
|
|
|
pub use self::TypeBound::*;
|
2013-08-15 16:28:54 -04:00
|
|
|
|
|
|
|
use syntax;
|
2013-09-05 10:14:35 -04:00
|
|
|
use syntax::codemap::Span;
|
2015-04-07 14:22:55 -07:00
|
|
|
use syntax::abi;
|
2013-08-15 16:28:54 -04:00
|
|
|
use syntax::ast;
|
2014-06-26 11:37:39 -07:00
|
|
|
use syntax::attr;
|
2013-09-05 10:14:35 -04:00
|
|
|
use syntax::ast::{Ident, NodeId};
|
2014-05-18 16:56:13 +03:00
|
|
|
use syntax::ptr::P;
|
2014-05-16 10:15:33 -07:00
|
|
|
|
2013-08-15 16:28:54 -04:00
|
|
|
pub struct Module {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Option<Ident>,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-04-27 05:08:36 +09:00
|
|
|
pub where_outer: Span,
|
|
|
|
pub where_inner: Span,
|
2014-12-26 10:55:16 +02:00
|
|
|
pub extern_crates: Vec<ExternCrate>,
|
|
|
|
pub imports: Vec<Import>,
|
2014-03-28 10:27:24 -07: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 17:41:15 -07:00
|
|
|
pub constants: Vec<Constant>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub traits: Vec<Trait>,
|
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub impls: Vec<Impl>,
|
2015-02-07 14:24:34 +01:00
|
|
|
pub def_traits: Vec<DefaultImpl>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub foreigns: Vec<ast::ForeignMod>,
|
|
|
|
pub macros: Vec<Macro>,
|
|
|
|
pub is_crate: bool,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
2013-09-05 10:14:35 -04:00
|
|
|
pub fn new(name: Option<Ident>) -> Module {
|
2013-08-15 16:28:54 -04:00
|
|
|
Module {
|
|
|
|
name : name,
|
|
|
|
id: 0,
|
2014-04-15 18:02:58 -07:00
|
|
|
vis: ast::Inherited,
|
2014-06-26 11:37:39 -07:00
|
|
|
stab: None,
|
2014-04-27 05:08:36 +09:00
|
|
|
where_outer: syntax::codemap::DUMMY_SP,
|
|
|
|
where_inner: syntax::codemap::DUMMY_SP,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs : Vec::new(),
|
2014-12-26 10:55:16 +02:00
|
|
|
extern_crates: Vec::new(),
|
|
|
|
imports : Vec::new(),
|
2014-03-05 15:28:08 -08:00
|
|
|
structs : Vec::new(),
|
|
|
|
enums : Vec::new(),
|
|
|
|
fns : Vec::new(),
|
|
|
|
mods : Vec::new(),
|
|
|
|
typedefs : Vec::new(),
|
|
|
|
statics : Vec::new(),
|
2014-10-06 17:41:15 -07:00
|
|
|
constants : Vec::new(),
|
2014-03-05 15:28:08 -08:00
|
|
|
traits : Vec::new(),
|
|
|
|
impls : Vec::new(),
|
2015-01-27 00:35:03 +01:00
|
|
|
def_traits : Vec::new(),
|
2014-03-05 15:28:08 -08:00
|
|
|
foreigns : Vec::new(),
|
|
|
|
macros : Vec::new(),
|
2014-02-28 22:33:45 +01:00
|
|
|
is_crate : false,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Debug, Clone, RustcEncodable, RustcDecodable, Copy)]
|
2013-08-15 16:28:54 -04: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,
|
2014-01-09 15:05:33 +02:00
|
|
|
TraitBound(ast::TraitRef)
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Struct {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub id: NodeId,
|
|
|
|
pub struct_type: StructType,
|
|
|
|
pub name: Ident,
|
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub fields: Vec<ast::StructField>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub variants: Vec<Variant>,
|
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: NodeId,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub kind: ast::VariantKind,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub decl: ast::FnDecl,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub name: Ident,
|
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-12-09 10:36:46 -05:00
|
|
|
pub unsafety: ast::Unsafety,
|
2015-02-25 22:05:07 +02:00
|
|
|
pub constness: ast::Constness,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub generics: ast::Generics,
|
2015-04-07 14:22:55 -07:00
|
|
|
pub abi: abi::Abi,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Typedef {
|
2014-05-18 16:56:13 +03:00
|
|
|
pub ty: P<ast::Ty>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub gen: ast::Generics,
|
|
|
|
pub name: Ident,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Debug)]
|
2013-08-15 16:28:54 -04:00
|
|
|
pub struct Static {
|
2014-05-18 16:56:13 +03:00
|
|
|
pub type_: P<ast::Ty>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub mutability: ast::Mutability,
|
2014-05-18 16:56:13 +03:00
|
|
|
pub expr: P<ast::Expr>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub id: ast::NodeId,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
2014-10-06 17:41:15 -07:00
|
|
|
pub struct Constant {
|
|
|
|
pub type_: P<ast::Ty>,
|
|
|
|
pub expr: P<ast::Expr>,
|
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub stab: Option<attr::Stability>,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
2013-08-15 16:28:54 -04:00
|
|
|
pub struct Trait {
|
2014-12-09 19:59:20 -05:00
|
|
|
pub unsafety: ast::Unsafety,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
2015-03-05 04:48:54 +02:00
|
|
|
pub items: Vec<P<ast::TraitItem>>, //should be TraitItem
|
2014-03-28 10:27:24 -07:00
|
|
|
pub generics: ast::Generics,
|
2014-08-27 21:46:52 -04:00
|
|
|
pub bounds: Vec<ast::TyParamBound>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: ast::NodeId,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
2014-12-10 06:15:06 -05:00
|
|
|
pub unsafety: ast::Unsafety,
|
2014-12-28 23:33:18 +01:00
|
|
|
pub polarity: ast::ImplPolarity,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub trait_: Option<ast::TraitRef>,
|
2014-05-18 16:56:13 +03:00
|
|
|
pub for_: P<ast::Ty>,
|
2015-03-05 04:48:54 +02:00
|
|
|
pub items: Vec<P<ast::ImplItem>>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub vis: ast::Visibility,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub id: ast::NodeId,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
2015-02-07 14:24:34 +01:00
|
|
|
pub struct DefaultImpl {
|
2015-01-27 00:35:03 +01:00
|
|
|
pub unsafety: ast::Unsafety,
|
|
|
|
pub trait_: ast::TraitRef,
|
|
|
|
pub id: ast::NodeId,
|
2015-03-12 19:15:52 -07:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub whence: Span,
|
2015-01-27 00:35:03 +01:00
|
|
|
}
|
|
|
|
|
2014-02-16 21:40:26 -08:00
|
|
|
pub struct Macro {
|
2014-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-06-26 11:37:39 -07:00
|
|
|
pub stab: Option<attr::Stability>,
|
2015-04-13 15:25:40 -07:00
|
|
|
pub imported_from: Option<Ident>,
|
2014-02-16 21:40:26 -08:00
|
|
|
}
|
|
|
|
|
2014-12-26 10:55:16 +02:00
|
|
|
pub struct ExternCrate {
|
|
|
|
pub name: Ident,
|
|
|
|
pub path: Option<String>,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Import {
|
|
|
|
pub id: NodeId,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub node: ast::ViewPath_,
|
|
|
|
pub whence: Span,
|
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
|
2013-08-15 16:28:54 -04:00
|
|
|
if sd.ctor_id.is_some() {
|
|
|
|
// We are in a tuple-struct
|
|
|
|
match sd.fields.len() {
|
|
|
|
0 => Unit,
|
|
|
|
1 => Newtype,
|
|
|
|
_ => Tuple
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Plain
|
|
|
|
}
|
|
|
|
}
|