Skip to content

Commit

Permalink
Remove intrinsic, consolidate TyDesc defns, remove ty_type.
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Feb 26, 2013
1 parent 181d1de commit bdaac46
Show file tree
Hide file tree
Showing 32 changed files with 385 additions and 514 deletions.
4 changes: 2 additions & 2 deletions src/libcore/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use vec;
#[abi = "cdecl"]
pub extern mod rustrt {
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TyDesc,
++v: **vec::raw::VecRepr,
++n: libc::size_t);
}
Expand Down Expand Up @@ -248,7 +248,7 @@ pub mod raw {
// Only make the (slow) call into the runtime if we have to
if capacity(*v) < n {
let ptr: **VecRepr = transmute(v);
rustrt::vec_reserve_shared_actual(sys::get_type_desc::<T>(),
rustrt::vec_reserve_shared_actual(sys::get_tydesc::<T>(),
ptr, n as libc::size_t);
}
}
Expand Down
21 changes: 7 additions & 14 deletions src/libcore/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@

use libc::{c_char, c_void, c_uint, intptr_t, uintptr_t};
use ptr::{mut_null, null, to_unsafe_ptr};
use repr::BoxRepr;
use sys::TypeDesc;
use managed::raw::BoxRepr;
use sys::{GlueFn, TyDesc};
use cast::transmute;

/**
* Runtime structures
*
* NB: These must match the representation in the C++ runtime.
*/

pub type DropGlue = fn(**TypeDesc, *c_void);
pub type FreeGlue = fn(**TypeDesc, *c_void);

pub type TaskID = uintptr_t;

#[cfg(target_word_size = "64")]
Expand Down Expand Up @@ -127,6 +118,7 @@ pub struct Task {
boxed_region: BoxedRegion
}


/*
* Box annihilation
*
Expand Down Expand Up @@ -202,9 +194,10 @@ pub unsafe fn annihilate() {
// Pass 2: Drop all boxes.
for each_live_alloc |box, uniq| {
if !uniq {
let tydesc: *TypeDesc = transmute(copy (*box).header.type_desc);
let drop_glue: DropGlue = transmute(((*tydesc).drop_glue, 0));
drop_glue(to_unsafe_ptr(&tydesc), transmute(&(*box).data));
let tydesc: *TyDesc = transmute((*box).header.type_desc);
let drop_glue: GlueFn = transmute(((*tydesc).drop_glue, 0));
drop_glue(to_unsafe_ptr(&tydesc),
transmute(&(*box).data));
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/libcore/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ pub mod raw {
pub const RC_MANAGED_UNIQUE : uint = (-2) as uint;
pub const RC_IMMORTAL : uint = 0x77777777;

#[cfg(stage0)]
use intrinsic::TyDesc;

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use sys::TyDesc;

pub struct BoxHeaderRepr {
ref_count: uint,
type_desc: *TyDesc,
Expand Down
11 changes: 4 additions & 7 deletions src/libcore/private/exchange_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use sys::{TypeDesc, size_of};
use sys::{TyDesc, size_of};
use libc::{c_void, size_t, uintptr_t};
use c_malloc = libc::malloc;
use c_free = libc::free;
use managed::raw::{BoxHeaderRepr, BoxRepr};
use cast::transmute;
use private::intrinsics::{atomic_xadd,atomic_xsub};
use ptr::null;
use intrinsic::TyDesc;
use sys::TyDesc;

pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
pub unsafe fn malloc(td: *TyDesc, size: uint) -> *c_void {
unsafe {
assert td.is_not_null();

let total_size = get_box_size(size, (*td).align);
let p = c_malloc(total_size as size_t);
assert p.is_not_null();

// FIXME #3475: Converting between our two different tydesc types
let td: *TyDesc = transmute(td);

let box: &mut BoxRepr = transmute(p);
box.header.ref_count = -1; // Exchange values not ref counted
box.header.type_desc = td;
box.header.type_desc = transmute(td);
box.header.prev = null();
box.header.next = null();

Expand Down
10 changes: 9 additions & 1 deletion src/libcore/private/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ as mentioned in #3369
The intrinsics are defined in librustc/middle/trans/foreign.rs.
*/

#[cfg(stage0)]
type TyDesc = ();

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use sys::TyDesc;

#[abi = "rust-intrinsic"]
pub extern {
pub fn atomic_cxchg(dst: &mut int, old: int, src: int) -> int;
Expand All @@ -40,7 +48,7 @@ pub extern {
pub fn min_align_of<T>() -> uint;
pub fn pref_align_of<T>() -> uint;

pub fn get_tydesc<T>() -> *();
pub fn get_tydesc<T>() -> *TyDesc;

pub fn init<T>() -> T;

Expand Down
136 changes: 134 additions & 2 deletions src/libcore/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,141 @@ Runtime type reflection
*/

use intrinsic::{TyDesc, get_tydesc, visit_tydesc, TyVisitor};
use libc::c_void;
use sys;
#[cfg(stage0)]
use intrinsic::{TyDesc, get_tydesc};

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use sys::{TyDesc, get_tydesc};

use libc::c_void;
use vec;

#[cfg(stage0)]
use intrinsic::{TyVisitor};

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
#[abi = "rust-intrinsic"]
extern mod rusti {
fn visit_tydesc(++td: *TyDesc, &&tv: TyVisitor);
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
/// Invoke `tv.visit_*` for the type described by `td`
pub fn visit_tydesc(++td: *TyDesc, &&tv: TyVisitor) {
unsafe { rusti::visit_tydesc(td, tv) }
}

/**
* Primary reflection callback interface. The compiler generates
* code for this; callers can use it to reflect on a type via
* `sys::get_tydesc` and `visit_tydesc` in this module.
*
* This interface is quite low-level and most callers will want
* to use a higher-level interface such as `MovePtrAdaptor`.
*/
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
#[lang="tyvisitor"]
pub trait TyVisitor {
fn visit_bot(&self) -> bool;
fn visit_nil(&self) -> bool;
fn visit_bool(&self) -> bool;

fn visit_int(&self) -> bool;
fn visit_i8(&self) -> bool;
fn visit_i16(&self) -> bool;
fn visit_i32(&self) -> bool;
fn visit_i64(&self) -> bool;

fn visit_uint(&self) -> bool;
fn visit_u8(&self) -> bool;
fn visit_u16(&self) -> bool;
fn visit_u32(&self) -> bool;
fn visit_u64(&self) -> bool;

fn visit_float(&self) -> bool;
fn visit_f32(&self) -> bool;
fn visit_f64(&self) -> bool;

fn visit_char(&self) -> bool;
fn visit_str(&self) -> bool;

fn visit_estr_box(&self) -> bool;
fn visit_estr_uniq(&self) -> bool;
fn visit_estr_slice(&self) -> bool;
fn visit_estr_fixed(&self, n: uint, sz: uint, align: uint) -> bool;

fn visit_box(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_uniq(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_ptr(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_rptr(&self, mtbl: uint, inner: *TyDesc) -> bool;

fn visit_vec(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_unboxed_vec(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_evec_box(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_evec_uniq(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_evec_slice(&self, mtbl: uint, inner: *TyDesc) -> bool;
fn visit_evec_fixed(&self, n: uint, sz: uint, align: uint,
mtbl: uint, inner: *TyDesc) -> bool;

fn visit_enter_rec(&self, n_fields: uint,
sz: uint, align: uint) -> bool;
fn visit_rec_field(&self, i: uint, name: &str,
mtbl: uint, inner: *TyDesc) -> bool;
fn visit_leave_rec(&self, n_fields: uint,
sz: uint, align: uint) -> bool;

fn visit_enter_class(&self, n_fields: uint,
sz: uint, align: uint) -> bool;
fn visit_class_field(&self, i: uint, name: &str,
mtbl: uint, inner: *TyDesc) -> bool;
fn visit_leave_class(&self, n_fields: uint,
sz: uint, align: uint) -> bool;

fn visit_enter_tup(&self, n_fields: uint,
sz: uint, align: uint) -> bool;
fn visit_tup_field(&self, i: uint, inner: *TyDesc) -> bool;
fn visit_leave_tup(&self, n_fields: uint,
sz: uint, align: uint) -> bool;

fn visit_enter_enum(&self, n_variants: uint,
sz: uint, align: uint) -> bool;
fn visit_enter_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool;
fn visit_enum_variant_field(&self, i: uint, inner: *TyDesc) -> bool;
fn visit_leave_enum_variant(&self, variant: uint,
disr_val: int,
n_fields: uint,
name: &str) -> bool;
fn visit_leave_enum(&self, n_variants: uint,
sz: uint, align: uint) -> bool;

fn visit_enter_fn(&self, purity: uint, proto: uint,
n_inputs: uint, retstyle: uint) -> bool;
fn visit_fn_input(&self, i: uint, mode: uint, inner: *TyDesc) -> bool;
fn visit_fn_output(&self, retstyle: uint, inner: *TyDesc) -> bool;
fn visit_leave_fn(&self, purity: uint, proto: uint,
n_inputs: uint, retstyle: uint) -> bool;

fn visit_trait(&self) -> bool;
fn visit_var(&self) -> bool;
fn visit_var_integral(&self) -> bool;
fn visit_param(&self, i: uint) -> bool;
fn visit_self(&self) -> bool;
fn visit_opaque_box(&self) -> bool;
fn visit_closure_ptr(&self, ck: uint) -> bool;
}

/**
* Trait for visitor that wishes to reflect on data. To use this, create a
* struct that encapsulates the set of pointers you wish to walk through a
Expand Down Expand Up @@ -471,6 +601,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
true
}

#[cfg(stage0)]
fn visit_type(&self) -> bool {
if ! self.inner.visit_type() { return false; }
true
Expand All @@ -483,6 +614,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
true
}

#[cfg(stage0)]
fn visit_constr(&self, inner: *TyDesc) -> bool {
if ! self.inner.visit_constr(inner) { return false; }
true
Expand Down
36 changes: 32 additions & 4 deletions src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,33 @@ use cast::transmute;
use cast;
use char;
use dvec::DVec;
use intrinsic;
use intrinsic::{TyDesc, TyVisitor, visit_tydesc};
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use sys::TyDesc;
use io;
use io::{Writer, WriterUtil};
use libc::c_void;
use managed;
use managed::raw::BoxHeaderRepr;
use ptr;
use reflect;

#[cfg(stage0)]
use reflect::{MovePtr, MovePtrAdaptor, align};
#[cfg(stage0)]
use intrinsic;
#[cfg(stage0)]
use intrinsic::{TyDesc, TyVisitor, visit_tydesc};

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use reflect::{TyVisitor, MovePtr, MovePtrAdaptor, visit_tydesc, align};

use repr;
use str;
use sys;
use sys::TypeDesc;
use to_str::ToStr;
use uint;
use vec::UnboxedVecRepr;
Expand Down Expand Up @@ -548,6 +561,7 @@ impl TyVisitor for ReprVisitor {
fn visit_var_integral(&self) -> bool { true }
fn visit_param(&self, _i: uint) -> bool { true }
fn visit_self(&self) -> bool { true }
#[cfg(stage0)]
fn visit_type(&self) -> bool { true }

fn visit_opaque_box(&self) -> bool {
Expand All @@ -558,12 +572,13 @@ impl TyVisitor for ReprVisitor {
}
}

// Type no longer exists, vestigial function.
#[cfg(stage0)]
fn visit_constr(&self, _inner: *TyDesc) -> bool { fail!(); }

fn visit_closure_ptr(&self, _ck: uint) -> bool { true }
}

#[cfg(stage0)]
pub fn write_repr<T>(writer: @Writer, object: &T) {
unsafe {
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
Expand All @@ -574,6 +589,19 @@ pub fn write_repr<T>(writer: @Writer, object: &T) {
}
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
pub fn write_repr<T>(writer: @Writer, object: &T) {
unsafe {
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
let tydesc = sys::get_tydesc::<T>();
let mut u = ReprVisitor(ptr, writer);
let v = reflect::MovePtrAdaptor(u);
visit_tydesc(tydesc, (v) as @TyVisitor)
}
}

#[test]
struct P {a: int, b: float}

Expand Down
Loading

0 comments on commit bdaac46

Please sign in to comment.