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.
|
|
|
|
|
|
|
|
use syntax;
|
2013-09-05 09:14:35 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-08-15 15:28:54 -05:00
|
|
|
use syntax::ast;
|
2013-09-05 09:14:35 -05:00
|
|
|
use syntax::ast::{Ident, NodeId};
|
2013-08-15 15:28:54 -05:00
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
use std::gc::Gc;
|
|
|
|
|
2013-08-15 15:28:54 -05:00
|
|
|
pub struct Module {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub name: Option<Ident>,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
2014-04-26 15:08:36 -05:00
|
|
|
pub where_outer: Span,
|
|
|
|
pub where_inner: Span,
|
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>,
|
|
|
|
pub traits: Vec<Trait>,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub impls: Vec<Impl>,
|
|
|
|
pub foreigns: Vec<ast::ForeignMod>,
|
|
|
|
pub view_items: Vec<ast::ViewItem>,
|
|
|
|
pub macros: Vec<Macro>,
|
|
|
|
pub is_crate: bool,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Module {
|
2013-09-05 09:14:35 -05:00
|
|
|
pub fn new(name: Option<Ident>) -> Module {
|
2013-08-15 15:28:54 -05:00
|
|
|
Module {
|
|
|
|
name : name,
|
|
|
|
id: 0,
|
2014-04-15 20:02:58 -05:00
|
|
|
vis: ast::Inherited,
|
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(),
|
|
|
|
structs : Vec::new(),
|
|
|
|
enums : Vec::new(),
|
|
|
|
fns : Vec::new(),
|
|
|
|
mods : Vec::new(),
|
|
|
|
typedefs : Vec::new(),
|
|
|
|
statics : Vec::new(),
|
|
|
|
traits : Vec::new(),
|
|
|
|
impls : Vec::new(),
|
|
|
|
view_items : Vec::new(),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
#[deriving(Show, Clone, Encodable, Decodable)]
|
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,
|
2014-01-09 07:05:33 -06:00
|
|
|
TraitBound(ast::TraitRef)
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Struct {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub struct_type: StructType,
|
|
|
|
pub name: Ident,
|
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub fields: Vec<ast::StructField>,
|
|
|
|
pub where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub variants: Vec<Variant>,
|
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub where: Span,
|
|
|
|
pub name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub kind: ast::VariantKind,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub decl: ast::FnDecl,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub name: Ident,
|
|
|
|
pub vis: ast::Visibility,
|
2014-04-06 20:04:40 -05:00
|
|
|
pub fn_style: ast::FnStyle,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub where: Span,
|
|
|
|
pub generics: ast::Generics,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Typedef {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub ty: ast::P<ast::Ty>,
|
|
|
|
pub gen: ast::Generics,
|
|
|
|
pub name: Ident,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub where: Span,
|
|
|
|
pub vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Static {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub type_: ast::P<ast::Ty>,
|
|
|
|
pub mutability: ast::Mutability,
|
2014-05-16 12:15:33 -05:00
|
|
|
pub expr: Gc<ast::Expr>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub name: Ident,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Trait {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub name: Ident,
|
|
|
|
pub methods: Vec<ast::TraitMethod>, //should be TraitMethod
|
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub parents: Vec<ast::TraitRef>,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub where: Span,
|
|
|
|
pub vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub generics: ast::Generics,
|
|
|
|
pub trait_: Option<ast::TraitRef>,
|
|
|
|
pub for_: ast::P<ast::Ty>,
|
2014-05-16 12:15:33 -05:00
|
|
|
pub methods: Vec<Gc<ast::Method>>,
|
2014-03-28 12:27:24 -05:00
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub where: Span,
|
|
|
|
pub vis: ast::Visibility,
|
|
|
|
pub id: ast::NodeId,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
2014-02-16 23:40:26 -06:00
|
|
|
pub struct Macro {
|
2014-03-28 12:27:24 -05:00
|
|
|
pub name: Ident,
|
|
|
|
pub id: ast::NodeId,
|
|
|
|
pub attrs: Vec<ast::Attribute>,
|
|
|
|
pub where: Span,
|
2014-02-16 23:40:26 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn struct_type_from_def(sd: &ast::StructDef) -> StructType {
|
2013-08-15 15:28:54 -05:00
|
|
|
if sd.ctor_id.is_some() {
|
|
|
|
// We are in a tuple-struct
|
|
|
|
match sd.fields.len() {
|
|
|
|
0 => Unit,
|
|
|
|
1 => Newtype,
|
|
|
|
_ => Tuple
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Plain
|
|
|
|
}
|
|
|
|
}
|