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.
|
|
|
|
|
|
|
|
use syntax;
|
2013-09-05 10:14:35 -04:00
|
|
|
use syntax::codemap::Span;
|
2013-08-15 16:28:54 -04:00
|
|
|
use syntax::ast;
|
2013-09-05 10:14:35 -04:00
|
|
|
use syntax::ast::{Ident, NodeId};
|
2013-08-15 16:28:54 -04:00
|
|
|
|
|
|
|
pub struct Module {
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Option<Ident>,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2014-03-05 15:28:08 -08:00
|
|
|
structs: Vec<Struct> ,
|
|
|
|
enums: Vec<Enum> ,
|
|
|
|
fns: Vec<Function> ,
|
|
|
|
mods: Vec<Module> ,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: NodeId,
|
2014-03-05 15:28:08 -08:00
|
|
|
typedefs: Vec<Typedef> ,
|
|
|
|
statics: Vec<Static> ,
|
|
|
|
traits: Vec<Trait> ,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2014-03-05 15:28:08 -08:00
|
|
|
impls: Vec<Impl> ,
|
|
|
|
foreigns: Vec<ast::ForeignMod> ,
|
|
|
|
view_items: Vec<ast::ViewItem> ,
|
|
|
|
macros: Vec<Macro> ,
|
2014-02-28 22:33:45 +01:00
|
|
|
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-01-09 22:25:09 +02:00
|
|
|
vis: ast::Private,
|
2014-01-01 15:53:22 +09:00
|
|
|
where: syntax::codemap::DUMMY_SP,
|
2014-03-05 15:28:08 -08: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 22:33:45 +01:00
|
|
|
is_crate : false,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
#[deriving(Show, Clone, Encodable, Decodable)]
|
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-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: NodeId,
|
|
|
|
struct_type: StructType,
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Ident,
|
2013-08-15 16:28:54 -04:00
|
|
|
generics: ast::Generics,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
|
|
|
fields: Vec<ast::StructField> ,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2014-03-05 15:28:08 -08:00
|
|
|
variants: Vec<Variant> ,
|
2013-08-15 16:28:54 -04:00
|
|
|
generics: ast::Generics,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: NodeId,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
|
|
|
name: Ident,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Ident,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2014-01-09 15:05:33 +02:00
|
|
|
kind: ast::VariantKind,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: ast::NodeId,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function {
|
2014-01-09 15:05:33 +02:00
|
|
|
decl: ast::FnDecl,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: NodeId,
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Ident,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
|
|
|
purity: ast::Purity,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
generics: ast::Generics,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Typedef {
|
2013-12-01 00:00:39 +02:00
|
|
|
ty: ast::P<ast::Ty>,
|
2013-08-15 16:28:54 -04:00
|
|
|
gen: ast::Generics,
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Ident,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: ast::NodeId,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Static {
|
2013-12-01 00:00:39 +02:00
|
|
|
type_: ast::P<ast::Ty>,
|
2013-09-05 10:14:35 -04:00
|
|
|
mutability: ast::Mutability,
|
|
|
|
expr: @ast::Expr,
|
|
|
|
name: Ident,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: ast::NodeId,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Trait {
|
2013-09-05 10:14:35 -04:00
|
|
|
name: Ident,
|
2014-03-05 15:28:08 -08:00
|
|
|
methods: Vec<ast::TraitMethod> , //should be TraitMethod
|
2013-08-15 16:28:54 -04:00
|
|
|
generics: ast::Generics,
|
2014-03-05 15:28:08 -08:00
|
|
|
parents: Vec<ast::TraitRef> ,
|
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: ast::NodeId,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
|
|
|
generics: ast::Generics,
|
2014-01-09 15:05:33 +02:00
|
|
|
trait_: Option<ast::TraitRef>,
|
2013-12-01 00:00:39 +02:00
|
|
|
for_: ast::P<ast::Ty>,
|
2014-03-05 15:28:08 -08:00
|
|
|
methods: Vec<@ast::Method> ,
|
|
|
|
attrs: Vec<ast::Attribute> ,
|
2013-09-05 10:14:35 -04:00
|
|
|
where: Span,
|
2014-01-09 15:05:33 +02:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 16:28:54 -04:00
|
|
|
id: ast::NodeId,
|
|
|
|
}
|
|
|
|
|
2014-02-16 21:40:26 -08:00
|
|
|
pub struct Macro {
|
|
|
|
name: Ident,
|
|
|
|
id: ast::NodeId,
|
2014-03-05 15:28:08 -08:00
|
|
|
attrs: Vec<ast::Attribute> ,
|
2014-02-16 21:40:26 -08:00
|
|
|
where: 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
|
|
|
|
}
|
|
|
|
}
|