Add core::reflect, start migrating core::repr to use it. Tidy up various Repr types.
This commit is contained in:
parent
1c5db46f8e
commit
aa2ab2659f
|
@ -30,7 +30,7 @@ pure fn capacity<T>(&&v: @[const T]) -> uint {
|
|||
unsafe {
|
||||
let repr: **unsafe::VecRepr =
|
||||
::unsafe::reinterpret_cast(&addr_of(v));
|
||||
(**repr).alloc / sys::size_of::<T>()
|
||||
(**repr).unboxed.alloc / sys::size_of::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -155,14 +155,14 @@ mod unsafe {
|
|||
#[inline(always)]
|
||||
unsafe fn set_len<T>(&&v: @[const T], new_len: uint) {
|
||||
let repr: **VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
(**repr).fill = new_len * sys::size_of::<T>();
|
||||
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
unsafe fn push<T>(&v: @[const T], +initval: T) {
|
||||
let repr: **VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
let fill = (**repr).fill;
|
||||
if (**repr).alloc > fill {
|
||||
let fill = (**repr).unboxed.fill;
|
||||
if (**repr).unboxed.alloc > fill {
|
||||
push_fast(v, move initval);
|
||||
}
|
||||
else {
|
||||
|
@ -173,9 +173,9 @@ mod unsafe {
|
|||
#[inline(always)] // really pretty please
|
||||
unsafe fn push_fast<T>(&v: @[const T], +initval: T) {
|
||||
let repr: **VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
let fill = (**repr).fill;
|
||||
(**repr).fill += sys::size_of::<T>();
|
||||
let p = ptr::addr_of((**repr).data);
|
||||
let fill = (**repr).unboxed.fill;
|
||||
(**repr).unboxed.fill += sys::size_of::<T>();
|
||||
let p = ptr::addr_of((**repr).unboxed.data);
|
||||
let p = ptr::offset(p, fill) as *mut T;
|
||||
rusti::move_val_init(*p, move initval);
|
||||
}
|
||||
|
|
|
@ -5,8 +5,25 @@
|
|||
#[forbid(deprecated_pattern)];
|
||||
|
||||
use cmp::{Eq, Ord};
|
||||
use intrinsic::TyDesc;
|
||||
|
||||
export ptr_eq;
|
||||
export ptr_eq, raw;
|
||||
|
||||
mod raw {
|
||||
|
||||
struct BoxHeaderRepr {
|
||||
ref_count: uint,
|
||||
type_desc: *TyDesc,
|
||||
prev: *BoxRepr,
|
||||
next: *BoxRepr,
|
||||
}
|
||||
|
||||
struct BoxRepr {
|
||||
header: BoxHeaderRepr,
|
||||
data: u8
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
|
||||
//! Determine if two shared boxes point to the same object
|
||||
|
|
|
@ -80,8 +80,8 @@ pub unsafe fn annihilate() {
|
|||
let mut box: *mut BoxRepr = transmute(copy box);
|
||||
while box != mut_null() {
|
||||
debug!("making box immortal: %x", box as uint);
|
||||
(*box).ref_count = 0x77777777;
|
||||
box = transmute(copy (*box).next);
|
||||
(*box).header.ref_count = 0x77777777;
|
||||
box = transmute(copy (*box).header.next);
|
||||
}
|
||||
|
||||
// Pass 2: Drop all boxes.
|
||||
|
@ -89,11 +89,11 @@ pub unsafe fn annihilate() {
|
|||
let mut box: *mut BoxRepr = transmute(copy box);
|
||||
while box != mut_null() {
|
||||
debug!("calling drop glue for box: %x", box as uint);
|
||||
let tydesc: *TypeDesc = transmute(copy (*box).type_desc);
|
||||
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));
|
||||
|
||||
box = transmute(copy (*box).next);
|
||||
box = transmute(copy (*box).header.next);
|
||||
}
|
||||
|
||||
// Pass 3: Free all boxes.
|
||||
|
@ -101,7 +101,7 @@ pub unsafe fn annihilate() {
|
|||
let box = (*task).boxed_region.live_allocs;
|
||||
if box == null() { break; }
|
||||
let mut box: *mut BoxRepr = transmute(copy box);
|
||||
assert (*box).prev == null();
|
||||
assert (*box).header.prev == null();
|
||||
|
||||
debug!("freeing box: %x", box as uint);
|
||||
rt_free(transmute(box));
|
||||
|
|
|
@ -63,6 +63,7 @@ export unit;
|
|||
export uniq;
|
||||
export repr;
|
||||
export cleanup;
|
||||
export reflect;
|
||||
|
||||
// NDM seems to be necessary for resolve to work
|
||||
export option_iter;
|
||||
|
@ -230,6 +231,7 @@ mod mutable;
|
|||
mod flate;
|
||||
mod repr;
|
||||
mod cleanup;
|
||||
mod reflect;
|
||||
|
||||
// Modules supporting compiler-generated code
|
||||
// Exported but not part of the public interface
|
||||
|
|
|
@ -0,0 +1,472 @@
|
|||
/*!
|
||||
* Helper types for interfacing with the `intrinsic::visit_ty`
|
||||
* reflection system.
|
||||
*/
|
||||
|
||||
use intrinsic::{TyDesc, get_tydesc, visit_tydesc, TyVisitor};
|
||||
use libc::c_void;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* data structure, and implement both `MovePtr` for it as well as `TyVisitor`;
|
||||
* then build a MovePtrAdaptor wrapped around your struct.
|
||||
*/
|
||||
trait MovePtr {
|
||||
fn move_ptr(adjustment: fn(*c_void) -> *c_void);
|
||||
}
|
||||
|
||||
/// Helper function for alignment calculation.
|
||||
#[inline(always)]
|
||||
fn align(size: uint, align: uint) -> uint {
|
||||
((size + align) - 1u) & !(align - 1u)
|
||||
}
|
||||
|
||||
/// Adaptor to wrap around visitors implementing MovePtr.
|
||||
struct MovePtrAdaptor<V: TyVisitor MovePtr> {
|
||||
inner: V
|
||||
}
|
||||
pub fn MovePtrAdaptor<V: TyVisitor MovePtr>(+v: V) -> MovePtrAdaptor<V> {
|
||||
MovePtrAdaptor { inner: move v }
|
||||
}
|
||||
|
||||
/// Abstract type-directed pointer-movement using the MovePtr trait
|
||||
impl<V: TyVisitor MovePtr> MovePtrAdaptor<V>: TyVisitor {
|
||||
|
||||
#[inline(always)]
|
||||
fn bump(sz: uint) {
|
||||
do self.inner.move_ptr() |p| {
|
||||
((p as uint) + sz) as *c_void
|
||||
};
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn align(a: uint) {
|
||||
do self.inner.move_ptr() |p| {
|
||||
align(p as uint, a) as *c_void
|
||||
};
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn align_to<T>() {
|
||||
self.align(sys::min_align_of::<T>());
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn bump_past<T>() {
|
||||
self.bump(sys::size_of::<T>());
|
||||
}
|
||||
|
||||
fn visit_bot() -> bool {
|
||||
self.align_to::<()>();
|
||||
if ! self.inner.visit_bot() { return false; }
|
||||
self.bump_past::<()>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_nil() -> bool {
|
||||
self.align_to::<()>();
|
||||
if ! self.inner.visit_nil() { return false; }
|
||||
self.bump_past::<()>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_bool() -> bool {
|
||||
self.align_to::<bool>();
|
||||
if ! self.inner.visit_bool() { return false; }
|
||||
self.bump_past::<bool>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_int() -> bool {
|
||||
self.align_to::<int>();
|
||||
if ! self.inner.visit_int() { return false; }
|
||||
self.bump_past::<int>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_i8() -> bool {
|
||||
self.align_to::<i8>();
|
||||
if ! self.inner.visit_i8() { return false; }
|
||||
self.bump_past::<i8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_i16() -> bool {
|
||||
self.align_to::<i16>();
|
||||
if ! self.inner.visit_i16() { return false; }
|
||||
self.bump_past::<i16>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_i32() -> bool {
|
||||
self.align_to::<i32>();
|
||||
if ! self.inner.visit_i32() { return false; }
|
||||
self.bump_past::<i32>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_i64() -> bool {
|
||||
self.align_to::<i64>();
|
||||
if ! self.inner.visit_i64() { return false; }
|
||||
self.bump_past::<i64>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_uint() -> bool {
|
||||
self.align_to::<uint>();
|
||||
if ! self.inner.visit_uint() { return false; }
|
||||
self.bump_past::<uint>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_u8() -> bool {
|
||||
self.align_to::<u8>();
|
||||
if ! self.inner.visit_u8() { return false; }
|
||||
self.bump_past::<u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_u16() -> bool {
|
||||
self.align_to::<u16>();
|
||||
if ! self.inner.visit_u16() { return false; }
|
||||
self.bump_past::<u16>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_u32() -> bool {
|
||||
self.align_to::<u32>();
|
||||
if ! self.inner.visit_u32() { return false; }
|
||||
self.bump_past::<u32>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_u64() -> bool {
|
||||
self.align_to::<u64>();
|
||||
if ! self.inner.visit_u64() { return false; }
|
||||
self.bump_past::<u64>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_float() -> bool {
|
||||
self.align_to::<float>();
|
||||
if ! self.inner.visit_float() { return false; }
|
||||
self.bump_past::<float>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_f32() -> bool {
|
||||
self.align_to::<f32>();
|
||||
if ! self.inner.visit_f32() { return false; }
|
||||
self.bump_past::<f32>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_f64() -> bool {
|
||||
self.align_to::<f64>();
|
||||
if ! self.inner.visit_f64() { return false; }
|
||||
self.bump_past::<f64>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_char() -> bool {
|
||||
self.align_to::<char>();
|
||||
if ! self.inner.visit_char() { return false; }
|
||||
self.bump_past::<char>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_str() -> bool {
|
||||
self.align_to::<~str>();
|
||||
if ! self.inner.visit_str() { return false; }
|
||||
self.bump_past::<~str>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_estr_box() -> bool {
|
||||
self.align_to::<@str>();
|
||||
if ! self.inner.visit_estr_box() { return false; }
|
||||
self.bump_past::<@str>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_estr_uniq() -> bool {
|
||||
self.align_to::<~str>();
|
||||
if ! self.inner.visit_estr_uniq() { return false; }
|
||||
self.bump_past::<~str>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_estr_slice() -> bool {
|
||||
self.align_to::<&static/str>();
|
||||
if ! self.inner.visit_estr_slice() { return false; }
|
||||
self.bump_past::<&static/str>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_estr_fixed(n: uint,
|
||||
sz: uint,
|
||||
align: uint) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_estr_fixed(n, sz, align) { return false; }
|
||||
self.bump(sz);
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_box(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<@u8>();
|
||||
if ! self.inner.visit_box(mtbl, inner) { return false; }
|
||||
self.bump_past::<@u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_uniq(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<~u8>();
|
||||
if ! self.inner.visit_uniq(mtbl, inner) { return false; }
|
||||
self.bump_past::<~u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_ptr(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<*u8>();
|
||||
if ! self.inner.visit_ptr(mtbl, inner) { return false; }
|
||||
self.bump_past::<*u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_rptr(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<&static/u8>();
|
||||
if ! self.inner.visit_rptr(mtbl, inner) { return false; }
|
||||
self.bump_past::<&static/u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_unboxed_vec(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<vec::raw::UnboxedVecRepr>();
|
||||
if ! self.inner.visit_vec(mtbl, inner) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_vec(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<~[u8]>();
|
||||
if ! self.inner.visit_vec(mtbl, inner) { return false; }
|
||||
self.bump_past::<~[u8]>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_evec_box(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<@[u8]>();
|
||||
if ! self.inner.visit_evec_box(mtbl, inner) { return false; }
|
||||
self.bump_past::<@[u8]>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_evec_uniq(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<~[u8]>();
|
||||
if ! self.inner.visit_evec_uniq(mtbl, inner) { return false; }
|
||||
self.bump_past::<~[u8]>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_evec_slice(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align_to::<&static/[u8]>();
|
||||
if ! self.inner.visit_evec_slice(mtbl, inner) { return false; }
|
||||
self.bump_past::<&static/[u8]>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_evec_fixed(n: uint, sz: uint, align: uint,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_evec_fixed(n, sz, align, mtbl, inner) {
|
||||
return false;
|
||||
}
|
||||
self.bump(sz);
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_rec(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_enter_rec(n_fields, sz, align) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_rec_field(i: uint, name: &str,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
unsafe { self.align((*inner).align); }
|
||||
if ! self.inner.visit_rec_field(i, name, mtbl, inner) {
|
||||
return false;
|
||||
}
|
||||
unsafe { self.bump((*inner).size); }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_rec(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
if ! self.inner.visit_leave_rec(n_fields, sz, align) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_class(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_enter_class(n_fields, sz, align) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_class_field(i: uint, name: &str,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
unsafe { self.align((*inner).align); }
|
||||
if ! self.inner.visit_class_field(i, name, mtbl, inner) {
|
||||
return false;
|
||||
}
|
||||
unsafe { self.bump((*inner).size); }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_class(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
if ! self.inner.visit_leave_class(n_fields, sz, align) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_tup(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_enter_tup(n_fields, sz, align) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_tup_field(i: uint, inner: *TyDesc) -> bool {
|
||||
unsafe { self.align((*inner).align); }
|
||||
if ! self.inner.visit_tup_field(i, inner) { return false; }
|
||||
unsafe { self.bump((*inner).size); }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_tup(n_fields: uint, sz: uint, align: uint) -> bool {
|
||||
if ! self.inner.visit_leave_tup(n_fields, sz, align) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_fn(purity: uint, proto: uint,
|
||||
n_inputs: uint, retstyle: uint) -> bool {
|
||||
if ! self.inner.visit_enter_fn(purity, proto, n_inputs, retstyle) {
|
||||
return false
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_fn_input(i: uint, mode: uint, inner: *TyDesc) -> bool {
|
||||
if ! self.inner.visit_fn_input(i, mode, inner) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_fn_output(retstyle: uint, inner: *TyDesc) -> bool {
|
||||
if ! self.inner.visit_fn_output(retstyle, inner) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_fn(purity: uint, proto: uint,
|
||||
n_inputs: uint, retstyle: uint) -> bool {
|
||||
if ! self.inner.visit_leave_fn(purity, proto, n_inputs, retstyle) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_enum(n_variants: uint, sz: uint, align: uint) -> bool {
|
||||
self.align(align);
|
||||
if ! self.inner.visit_enter_enum(n_variants, sz, align) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_enum_variant(variant: uint,
|
||||
disr_val: int,
|
||||
n_fields: uint,
|
||||
name: &str) -> bool {
|
||||
if ! self.inner.visit_enter_enum_variant(variant, disr_val,
|
||||
n_fields, name) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enum_variant_field(i: uint, inner: *TyDesc) -> bool {
|
||||
if ! self.inner.visit_enum_variant_field(i, inner) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_enum_variant(variant: uint,
|
||||
disr_val: int,
|
||||
n_fields: uint,
|
||||
name: &str) -> bool {
|
||||
if ! self.inner.visit_leave_enum_variant(variant, disr_val,
|
||||
n_fields, name) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_enum(n_variants: uint, sz: uint, align: uint) -> bool {
|
||||
if ! self.inner.visit_leave_enum(n_variants, sz, align) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_trait() -> bool {
|
||||
self.align_to::<TyVisitor>();
|
||||
if ! self.inner.visit_trait() { return false; }
|
||||
self.bump_past::<TyVisitor>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_var() -> bool {
|
||||
if ! self.inner.visit_var() { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_var_integral() -> bool {
|
||||
if ! self.inner.visit_var_integral() { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_param(i: uint) -> bool {
|
||||
if ! self.inner.visit_param(i) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_self() -> bool {
|
||||
self.align_to::<&static/u8>();
|
||||
if ! self.inner.visit_self() { return false; }
|
||||
self.align_to::<&static/u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_type() -> bool {
|
||||
if ! self.inner.visit_type() { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_opaque_box() -> bool {
|
||||
self.align_to::<@u8>();
|
||||
if ! self.inner.visit_opaque_box() { return false; }
|
||||
self.bump_past::<@u8>();
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_constr(inner: *TyDesc) -> bool {
|
||||
if ! self.inner.visit_constr(inner) { return false; }
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_closure_ptr(ck: uint) -> bool {
|
||||
self.align_to::<fn@()>();
|
||||
if ! self.inner.visit_closure_ptr(ck) { return false; }
|
||||
self.bump_past::<fn@()>();
|
||||
true
|
||||
}
|
||||
}
|
|
@ -4,7 +4,10 @@ use libc::c_void;
|
|||
use sys::TypeDesc;
|
||||
use to_str::ToStr;
|
||||
use unsafe::transmute;
|
||||
use intrinsic::{TyDesc, TyVisitor};
|
||||
use intrinsic::{TyDesc, TyVisitor, visit_tydesc};
|
||||
use reflect::{MovePtr, MovePtrAdaptor};
|
||||
use vec::raw::{VecRepr, UnboxedVecRepr, SliceRepr};
|
||||
use box::raw::{BoxRepr, BoxHeaderRepr};
|
||||
|
||||
/// Helpers
|
||||
|
||||
|
@ -14,21 +17,6 @@ fn align(size: uint, align: uint) -> uint {
|
|||
((size + align) - 1) & !(align - 1)
|
||||
}
|
||||
|
||||
struct VecRepr {
|
||||
box_header: (uint, uint, uint, uint),
|
||||
mut fill: uint,
|
||||
mut alloc: uint,
|
||||
data: u8
|
||||
}
|
||||
|
||||
struct BoxRepr {
|
||||
ref_count: uint,
|
||||
type_desc: *TyDesc,
|
||||
prev: *BoxRepr,
|
||||
next: *BoxRepr,
|
||||
data: u8
|
||||
}
|
||||
|
||||
trait EscapedCharWriter {
|
||||
fn write_escaped_char(ch: char);
|
||||
}
|
||||
|
@ -118,6 +106,397 @@ impl char : Repr {
|
|||
fn write_repr(writer: @Writer) { writer.write_char(self); }
|
||||
}
|
||||
|
||||
|
||||
// New implementation using reflect::MovePtr
|
||||
|
||||
struct ReprVisitor {
|
||||
mut ptr: *c_void,
|
||||
writer: @Writer
|
||||
}
|
||||
fn ReprVisitor(ptr: *c_void, writer: @Writer) -> ReprVisitor {
|
||||
ReprVisitor { ptr: ptr, writer: writer }
|
||||
}
|
||||
|
||||
impl ReprVisitor : MovePtr {
|
||||
#[inline(always)]
|
||||
fn move_ptr(adjustment: fn(*c_void) -> *c_void) {
|
||||
self.ptr = adjustment(self.ptr);
|
||||
}
|
||||
}
|
||||
|
||||
impl ReprVisitor {
|
||||
|
||||
// Various helpers for the TyVisitor impl
|
||||
|
||||
#[inline(always)]
|
||||
fn get<T>(f: fn((&T))) -> bool {
|
||||
unsafe {
|
||||
f(transmute::<*c_void,&T>(copy self.ptr));
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn visit_inner(inner: *TyDesc) -> bool {
|
||||
self.visit_ptr_inner(self.ptr, inner)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn visit_ptr_inner(ptr: *c_void, inner: *TyDesc) -> bool {
|
||||
let mut u = ReprVisitor(ptr, self.writer);
|
||||
let v = reflect::MovePtrAdaptor(move u);
|
||||
visit_tydesc(inner, v as @TyVisitor);
|
||||
true
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn write<T:Repr>() -> bool {
|
||||
do self.get |v:&T| {
|
||||
v.write_repr(self.writer);
|
||||
}
|
||||
}
|
||||
|
||||
fn write_escaped_slice(slice: &str) {
|
||||
self.writer.write_char('"');
|
||||
do str::chars_iter(slice) |ch| {
|
||||
self.writer.write_escaped_char(ch);
|
||||
}
|
||||
self.writer.write_char('"');
|
||||
}
|
||||
|
||||
fn write_mut_qualifier(mtbl: uint) {
|
||||
if mtbl == 0 {
|
||||
self.writer.write_str("mut ");
|
||||
} else if mtbl == 1 {
|
||||
// skip, this is ast::m_imm
|
||||
} else {
|
||||
assert mtbl == 2;
|
||||
self.writer.write_str("const ");
|
||||
}
|
||||
}
|
||||
|
||||
fn write_vec_range(mtbl: uint, ptr: *u8, len: uint,
|
||||
inner: *TyDesc) -> bool {
|
||||
let mut p = ptr;
|
||||
let end = ptr::offset(p, len);
|
||||
let (sz, al) = unsafe { ((*inner).size, (*inner).align) };
|
||||
self.writer.write_char('[');
|
||||
let mut first = true;
|
||||
while p as uint < end as uint {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
self.writer.write_str(", ");
|
||||
}
|
||||
self.write_mut_qualifier(mtbl);
|
||||
self.visit_ptr_inner(p as *c_void, inner);
|
||||
p = align(ptr::offset(p, sz) as uint, al) as *u8;
|
||||
}
|
||||
self.writer.write_char(']');
|
||||
true
|
||||
}
|
||||
|
||||
fn write_unboxed_vec_repr(mtbl: uint, v: &UnboxedVecRepr,
|
||||
inner: *TyDesc) -> bool {
|
||||
self.write_vec_range(mtbl, ptr::to_unsafe_ptr(&v.data),
|
||||
v.fill, inner)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl ReprVisitor : TyVisitor {
|
||||
fn visit_bot() -> bool {
|
||||
self.writer.write_str("!");
|
||||
true
|
||||
}
|
||||
fn visit_nil() -> bool { self.write::<()>() }
|
||||
fn visit_bool() -> bool { self.write::<bool>() }
|
||||
fn visit_int() -> bool { self.write::<int>() }
|
||||
fn visit_i8() -> bool { self.write::<i8>() }
|
||||
fn visit_i16() -> bool { self.write::<i16>() }
|
||||
fn visit_i32() -> bool { self.write::<i32>() }
|
||||
fn visit_i64() -> bool { self.write::<i64>() }
|
||||
|
||||
fn visit_uint() -> bool { self.write::<uint>() }
|
||||
fn visit_u8() -> bool { self.write::<u8>() }
|
||||
fn visit_u16() -> bool { self.write::<u16>() }
|
||||
fn visit_u32() -> bool { self.write::<u32>() }
|
||||
fn visit_u64() -> bool { self.write::<u64>() }
|
||||
|
||||
fn visit_float() -> bool { self.write::<float>() }
|
||||
fn visit_f32() -> bool { self.write::<f32>() }
|
||||
fn visit_f64() -> bool { self.write::<f64>() }
|
||||
|
||||
fn visit_char() -> bool { self.write::<uint>() }
|
||||
|
||||
// Type no longer exists, vestigial function.
|
||||
fn visit_str() -> bool { fail; }
|
||||
|
||||
fn visit_estr_box() -> bool {
|
||||
do self.get::<@str> |s| {
|
||||
self.writer.write_char('@');
|
||||
self.write_escaped_slice(*s);
|
||||
}
|
||||
}
|
||||
fn visit_estr_uniq() -> bool {
|
||||
do self.get::<~str> |s| {
|
||||
self.writer.write_char('~');
|
||||
self.write_escaped_slice(*s);
|
||||
}
|
||||
}
|
||||
fn visit_estr_slice() -> bool {
|
||||
do self.get::<&str> |s| {
|
||||
self.write_escaped_slice(*s);
|
||||
}
|
||||
}
|
||||
|
||||
// Type no longer exists, vestigial function.
|
||||
fn visit_estr_fixed(_n: uint, _sz: uint,
|
||||
_align: uint) -> bool { fail; }
|
||||
|
||||
fn visit_box(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.writer.write_char('@');
|
||||
self.write_mut_qualifier(mtbl);
|
||||
do self.get::<&box::raw::BoxRepr> |b| {
|
||||
let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
|
||||
self.visit_ptr_inner(p, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_uniq(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.writer.write_char('~');
|
||||
self.write_mut_qualifier(mtbl);
|
||||
do self.get::<&box::raw::BoxRepr> |b| {
|
||||
let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
|
||||
self.visit_ptr_inner(p, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_ptr(_mtbl: uint, _inner: *TyDesc) -> bool {
|
||||
do self.get::<*c_void> |p| {
|
||||
self.writer.write_str(fmt!("(0x%x as *())",
|
||||
*p as uint));
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_rptr(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
self.writer.write_char('&');
|
||||
self.write_mut_qualifier(mtbl);
|
||||
do self.get::<*c_void> |p| {
|
||||
self.visit_ptr_inner(*p, inner);
|
||||
}
|
||||
}
|
||||
|
||||
// Type no longer exists, vestigial function.
|
||||
fn visit_vec(_mtbl: uint, _inner: *TyDesc) -> bool { fail; }
|
||||
|
||||
|
||||
fn visit_unboxed_vec(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
do self.get::<vec::raw::UnboxedVecRepr> |b| {
|
||||
self.write_unboxed_vec_repr(mtbl, b, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_evec_box(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
do self.get::<&VecRepr> |b| {
|
||||
self.writer.write_char('@');
|
||||
self.write_unboxed_vec_repr(mtbl, &b.unboxed, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_evec_uniq(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
do self.get::<&VecRepr> |b| {
|
||||
self.writer.write_char('~');
|
||||
self.write_unboxed_vec_repr(mtbl, &b.unboxed, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_evec_slice(mtbl: uint, inner: *TyDesc) -> bool {
|
||||
do self.get::<SliceRepr> |s| {
|
||||
self.writer.write_char('&');
|
||||
self.write_vec_range(mtbl, s.data, s.len, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_evec_fixed(_n: uint, sz: uint, _align: uint,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
do self.get::<u8> |b| {
|
||||
self.write_vec_range(mtbl, ptr::to_unsafe_ptr(b), sz, inner);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_enter_rec(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char('{');
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_rec_field(i: uint, name: &str,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
if i != 0 {
|
||||
self.writer.write_str(", ");
|
||||
}
|
||||
self.write_mut_qualifier(mtbl);
|
||||
self.writer.write_str(name);
|
||||
self.writer.write_str(": ");
|
||||
self.visit_inner(inner);
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_leave_rec(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char('}');
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_class(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char('{');
|
||||
true
|
||||
}
|
||||
fn visit_class_field(i: uint, name: &str,
|
||||
mtbl: uint, inner: *TyDesc) -> bool {
|
||||
if i != 0 {
|
||||
self.writer.write_str(", ");
|
||||
}
|
||||
self.write_mut_qualifier(mtbl);
|
||||
self.writer.write_str(name);
|
||||
self.writer.write_str(": ");
|
||||
self.visit_inner(inner);
|
||||
true
|
||||
}
|
||||
fn visit_leave_class(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char('}');
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_tup(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char('(');
|
||||
true
|
||||
}
|
||||
fn visit_tup_field(i: uint, inner: *TyDesc) -> bool {
|
||||
if i != 0 {
|
||||
self.writer.write_str(", ");
|
||||
}
|
||||
self.visit_inner(inner);
|
||||
true
|
||||
}
|
||||
fn visit_leave_tup(_n_fields: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
self.writer.write_char(')');
|
||||
true
|
||||
}
|
||||
|
||||
fn visit_enter_enum(_n_variants: uint,
|
||||
_sz: uint, _align: uint) -> bool {
|
||||
true
|
||||
}
|
||||
fn visit_enter_enum_variant(_variant: uint,
|
||||
_disr_val: int,
|
||||
_n_fields: uint,
|
||||
_name: &str) -> bool { true }
|
||||
fn visit_enum_variant_field(_i: uint, inner: *TyDesc) -> bool {
|
||||
self.visit_inner(inner)
|
||||
}
|
||||
fn visit_leave_enum_variant(_variant: uint,
|
||||
_disr_val: int,
|
||||
_n_fields: uint,
|
||||
_name: &str) -> bool { true }
|
||||
fn visit_leave_enum(_n_variants: uint,
|
||||
_sz: uint, _align: uint) -> bool { true }
|
||||
|
||||
fn visit_enter_fn(_purity: uint, _proto: uint,
|
||||
_n_inputs: uint, _retstyle: uint) -> bool { true }
|
||||
fn visit_fn_input(_i: uint, _mode: uint, _inner: *TyDesc) -> bool { true }
|
||||
fn visit_fn_output(_retstyle: uint, _inner: *TyDesc) -> bool { true }
|
||||
fn visit_leave_fn(_purity: uint, _proto: uint,
|
||||
_n_inputs: uint, _retstyle: uint) -> bool { true }
|
||||
|
||||
|
||||
fn visit_trait() -> bool { true }
|
||||
fn visit_var() -> bool { true }
|
||||
fn visit_var_integral() -> bool { true }
|
||||
fn visit_param(_i: uint) -> bool { true }
|
||||
fn visit_self() -> bool { true }
|
||||
fn visit_type() -> bool { true }
|
||||
|
||||
fn visit_opaque_box() -> bool {
|
||||
self.writer.write_char('@');
|
||||
do self.get::<&box::raw::BoxRepr> |b| {
|
||||
let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
|
||||
self.visit_ptr_inner(p, b.header.type_desc);
|
||||
}
|
||||
}
|
||||
|
||||
// Type no longer exists, vestigial function.
|
||||
fn visit_constr(_inner: *TyDesc) -> bool { fail; }
|
||||
|
||||
fn visit_closure_ptr(_ck: uint) -> bool { true }
|
||||
}
|
||||
|
||||
pub fn write_repr2<T>(writer: @Writer, object: &T) {
|
||||
let ptr = ptr::to_unsafe_ptr(object) as *c_void;
|
||||
let tydesc = intrinsic::get_tydesc::<T>();
|
||||
let mut u = ReprVisitor(ptr, writer);
|
||||
let v = reflect::MovePtrAdaptor(move u);
|
||||
visit_tydesc(tydesc, v as @TyVisitor)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_repr2() {
|
||||
repr::write_repr2(io::stdout(), &10);
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &true);
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &false);
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &1.234);
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(&"hello"));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@"hello"));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(~"he\u10f3llo"));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@10));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@mut 10));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(~10));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(~mut 10));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(&10));
|
||||
io::println("");
|
||||
let mut x = 10;
|
||||
repr::write_repr2(io::stdout(), &(&mut x));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(ptr::to_unsafe_ptr(&10) as *int));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@[1,2,3,4,5,6,7,8]));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@[1u8,2u8,3u8,4u8]));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@["hi", "there"]));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(~["hi", "there"]));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(&["hi", "there"]));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &({a:10, b:1.234}));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(@{a:10, b:1.234}));
|
||||
io::println("");
|
||||
repr::write_repr2(io::stdout(), &(~{a:10, b:1.234}));
|
||||
io::println("");
|
||||
}
|
||||
|
||||
|
||||
// Old non-factored implementation, transitional...
|
||||
|
||||
enum EnumVisitState {
|
||||
PreVariant, // We're before the variant we're interested in.
|
||||
InVariant, // We're inside the variant we're interested in.
|
||||
|
@ -181,8 +560,8 @@ impl ReprPrinterWrapper {
|
|||
self.printer.writer.write_char('"');
|
||||
let vec_repr_ptr: **VecRepr = transmute(copy self.printer.ptr);
|
||||
let vec_repr = *vec_repr_ptr;
|
||||
let data_ptr = ptr::to_unsafe_ptr(&(*vec_repr).data);
|
||||
let slice: &str = transmute((data_ptr, (*vec_repr).fill));
|
||||
let data_ptr = ptr::to_unsafe_ptr(&(*vec_repr).unboxed.data);
|
||||
let slice: &str = transmute((data_ptr, (*vec_repr).unboxed.fill));
|
||||
do str::chars_iter(slice) |ch| {
|
||||
self.printer.writer.write_escaped_char(ch);
|
||||
}
|
||||
|
@ -199,7 +578,8 @@ impl ReprPrinterWrapper {
|
|||
let box_ptr: **BoxRepr = transmute(copy self.printer.ptr);
|
||||
let box = *box_ptr;
|
||||
self.printer.ptr = transmute(&(*box).data);
|
||||
intrinsic::visit_tydesc((*box).type_desc, self as @TyVisitor);
|
||||
intrinsic::visit_tydesc((*box).header.type_desc,
|
||||
self as @TyVisitor);
|
||||
let box_size = sys::size_of::<*BoxRepr>();
|
||||
self.printer.ptr = transmute(box_ptr as uint + box_size);
|
||||
true
|
||||
|
@ -229,9 +609,9 @@ impl ReprPrinterWrapper {
|
|||
let vec_repr_ptr: **VecRepr = transmute(copy self.printer.ptr);
|
||||
let old_ptr = self.printer.ptr as uint;
|
||||
let vec_repr: *VecRepr = *vec_repr_ptr;
|
||||
self.printer.ptr = transmute(&(*vec_repr).data);
|
||||
self.printer.ptr = transmute(&(*vec_repr).unboxed.data);
|
||||
let end_ptr: *c_void = transmute(self.printer.ptr as uint +
|
||||
(*vec_repr).fill);
|
||||
(*vec_repr).unboxed.fill);
|
||||
let sys_tydesc: *TypeDesc = transmute(copy inner);
|
||||
let alignment = (*sys_tydesc).align;
|
||||
let mut first = true;
|
||||
|
|
|
@ -2119,8 +2119,9 @@ mod raw {
|
|||
/// Sets the length of the string and adds the null terminator
|
||||
unsafe fn set_len(&v: ~str, new_len: uint) {
|
||||
let repr: *vec::raw::VecRepr = ::unsafe::reinterpret_cast(&v);
|
||||
(*repr).fill = new_len + 1u;
|
||||
let null = ptr::mut_offset(ptr::mut_addr_of((*repr).data), new_len);
|
||||
(*repr).unboxed.fill = new_len + 1u;
|
||||
let null = ptr::mut_offset(ptr::mut_addr_of((*repr).unboxed.data),
|
||||
new_len);
|
||||
*null = 0u8;
|
||||
}
|
||||
|
||||
|
|
|
@ -169,7 +169,7 @@ fn reserve_at_least<T>(&v: ~[const T], n: uint) {
|
|||
pure fn capacity<T>(&&v: ~[const T]) -> uint {
|
||||
unsafe {
|
||||
let repr: **raw::VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
(**repr).alloc / sys::size_of::<T>()
|
||||
(**repr).unboxed.alloc / sys::size_of::<T>()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -565,8 +565,8 @@ fn swap_remove<T>(&v: ~[const T], index: uint) -> T {
|
|||
fn push<T>(&v: ~[const T], +initval: T) {
|
||||
unsafe {
|
||||
let repr: **raw::VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
let fill = (**repr).fill;
|
||||
if (**repr).alloc > fill {
|
||||
let fill = (**repr).unboxed.fill;
|
||||
if (**repr).unboxed.alloc > fill {
|
||||
push_fast(v, move initval);
|
||||
}
|
||||
else {
|
||||
|
@ -579,9 +579,9 @@ fn push<T>(&v: ~[const T], +initval: T) {
|
|||
#[inline(always)] // really pretty please
|
||||
unsafe fn push_fast<T>(&v: ~[const T], +initval: T) {
|
||||
let repr: **raw::VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
let fill = (**repr).fill;
|
||||
(**repr).fill += sys::size_of::<T>();
|
||||
let p = ptr::addr_of((**repr).data);
|
||||
let fill = (**repr).unboxed.fill;
|
||||
(**repr).unboxed.fill += sys::size_of::<T>();
|
||||
let p = ptr::addr_of((**repr).unboxed.data);
|
||||
let p = ptr::offset(p, fill) as *mut T;
|
||||
rusti::move_val_init(*p, move initval);
|
||||
}
|
||||
|
@ -1787,13 +1787,19 @@ impl<T: Copy> &[T]: ImmutableCopyableVector<T> {
|
|||
/// Unsafe operations
|
||||
mod raw {
|
||||
// FIXME: This should have crate visibility (#1893 blocks that)
|
||||
/// The internal representation of a vector
|
||||
type VecRepr = {
|
||||
box_header: (uint, uint, uint, uint),
|
||||
|
||||
/// The internal representation of a (boxed) vector
|
||||
struct VecRepr {
|
||||
box_header: box::raw::BoxHeaderRepr,
|
||||
unboxed: UnboxedVecRepr
|
||||
}
|
||||
|
||||
/// The internal 'unboxed' representation of a vector
|
||||
struct UnboxedVecRepr {
|
||||
mut fill: uint,
|
||||
mut alloc: uint,
|
||||
data: u8
|
||||
};
|
||||
}
|
||||
|
||||
type SliceRepr = {
|
||||
mut data: *u8,
|
||||
|
@ -1827,7 +1833,7 @@ mod raw {
|
|||
#[inline(always)]
|
||||
unsafe fn set_len<T>(&&v: ~[const T], new_len: uint) {
|
||||
let repr: **VecRepr = ::unsafe::reinterpret_cast(&addr_of(v));
|
||||
(**repr).fill = new_len * sys::size_of::<T>();
|
||||
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,7 +12,6 @@ mod intrinsic {
|
|||
}
|
||||
|
||||
pub enum TyDesc = {
|
||||
first_param: **u8,
|
||||
size: uint,
|
||||
align: uint
|
||||
// Remaining fields not listed
|
||||
|
|
Loading…
Reference in New Issue