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
|
|
|
|
|
|
|
pub struct Module {
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Option<Ident>,
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
structs: ~[Struct],
|
|
|
|
enums: ~[Enum],
|
|
|
|
fns: ~[Function],
|
|
|
|
mods: ~[Module],
|
|
|
|
id: NodeId,
|
|
|
|
typedefs: ~[Typedef],
|
|
|
|
statics: ~[Static],
|
|
|
|
traits: ~[Trait],
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
impls: ~[Impl],
|
2014-01-09 07:05:33 -06:00
|
|
|
foreigns: ~[ast::ForeignMod],
|
|
|
|
view_items: ~[ast::ViewItem],
|
2014-02-16 23:40:26 -06:00
|
|
|
macros: ~[Macro],
|
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-01-09 14:25:09 -06:00
|
|
|
vis: ast::Private,
|
2014-01-01 00:53:22 -06:00
|
|
|
where: syntax::codemap::DUMMY_SP,
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs : ~[],
|
|
|
|
structs : ~[],
|
|
|
|
enums : ~[],
|
|
|
|
fns : ~[],
|
|
|
|
mods : ~[],
|
|
|
|
typedefs : ~[],
|
|
|
|
statics : ~[],
|
|
|
|
traits : ~[],
|
|
|
|
impls : ~[],
|
|
|
|
view_items : ~[],
|
2013-09-26 13:57:25 -05:00
|
|
|
foreigns : ~[],
|
2014-02-16 23:40:26 -06:00
|
|
|
macros : ~[],
|
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-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
id: NodeId,
|
|
|
|
struct_type: StructType,
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
generics: ast::Generics,
|
|
|
|
attrs: ~[ast::Attribute],
|
2014-01-09 07:05:33 -06:00
|
|
|
fields: ~[ast::StructField],
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Enum {
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
variants: ~[Variant],
|
|
|
|
generics: ast::Generics,
|
|
|
|
attrs: ~[ast::Attribute],
|
|
|
|
id: NodeId,
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
|
|
|
name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Variant {
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
2014-01-09 07:05:33 -06:00
|
|
|
kind: ast::VariantKind,
|
2013-08-15 15:28:54 -05:00
|
|
|
id: ast::NodeId,
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Function {
|
2014-01-09 07:05:33 -06:00
|
|
|
decl: ast::FnDecl,
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
|
|
|
id: NodeId,
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Ident,
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
|
|
|
purity: ast::Purity,
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
generics: ast::Generics,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Typedef {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: ast::P<ast::Ty>,
|
2013-08-15 15:28:54 -05:00
|
|
|
gen: ast::Generics,
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
id: ast::NodeId,
|
|
|
|
attrs: ~[ast::Attribute],
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Static {
|
2013-11-30 16:00:39 -06:00
|
|
|
type_: ast::P<ast::Ty>,
|
2013-09-05 09:14:35 -05:00
|
|
|
mutability: ast::Mutability,
|
|
|
|
expr: @ast::Expr,
|
|
|
|
name: Ident,
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
id: ast::NodeId,
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Trait {
|
2013-09-05 09:14:35 -05:00
|
|
|
name: Ident,
|
2014-01-09 07:05:33 -06:00
|
|
|
methods: ~[ast::TraitMethod], //should be TraitMethod
|
2013-08-15 15:28:54 -05:00
|
|
|
generics: ast::Generics,
|
2014-01-09 07:05:33 -06:00
|
|
|
parents: ~[ast::TraitRef],
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
|
|
|
id: ast::NodeId,
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
|
|
|
generics: ast::Generics,
|
2014-01-09 07:05:33 -06:00
|
|
|
trait_: Option<ast::TraitRef>,
|
2013-11-30 16:00:39 -06:00
|
|
|
for_: ast::P<ast::Ty>,
|
2014-01-09 07:05:33 -06:00
|
|
|
methods: ~[@ast::Method],
|
2013-08-15 15:28:54 -05:00
|
|
|
attrs: ~[ast::Attribute],
|
2013-09-05 09:14:35 -05:00
|
|
|
where: Span,
|
2014-01-09 07:05:33 -06:00
|
|
|
vis: ast::Visibility,
|
2013-08-15 15:28:54 -05:00
|
|
|
id: ast::NodeId,
|
|
|
|
}
|
|
|
|
|
2014-02-16 23:40:26 -06:00
|
|
|
pub struct Macro {
|
|
|
|
name: Ident,
|
|
|
|
id: ast::NodeId,
|
|
|
|
attrs: ~[ast::Attribute],
|
|
|
|
where: Span,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|