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;
|
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-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>,
|
|
|
|
pub foreigns: Vec<ast::ForeignMod>,
|
|
|
|
pub view_items: Vec<ast::ViewItem>,
|
|
|
|
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(),
|
|
|
|
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(),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
impl Copy for StructType {}
|
|
|
|
|
2013-08-15 16:28:54 -04:00
|
|
|
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-04-06 18:04:40 -07:00
|
|
|
pub fn_style: ast::FnStyle,
|
2014-08-11 09:32:26 -07:00
|
|
|
pub whence: Span,
|
2014-03-28 10:27:24 -07:00
|
|
|
pub generics: ast::Generics,
|
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
|
|
|
}
|
|
|
|
|
2014-10-15 03:20:39 -04:00
|
|
|
#[deriving(Show)]
|
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-03-28 10:27:24 -07:00
|
|
|
pub name: Ident,
|
2014-08-04 13:56:56 -07:00
|
|
|
pub items: Vec<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>,
|
2014-11-24 10:14:46 -08:00
|
|
|
pub default_unbound: Option<ast::TraitRef> // FIXME(tomjakubowski)
|
2013-08-15 16:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Impl {
|
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>,
|
2014-08-04 13:56:56 -07:00
|
|
|
pub items: Vec<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
|
|
|
}
|
|
|
|
|
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>,
|
2014-02-16 21:40:26 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|