diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-05-22 11:28:01 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-05-27 21:44:51 -0700 |
| commit | b53454e2e413ac58da20933968cb4a86a3c7c476 (patch) | |
| tree | 049f02aae3a1c7be395a3b7467ae31af92def081 /src/libdebug | |
| parent | 911cc9c35234ab12a4b9a6fc1cb35b52556f242d (diff) | |
| download | rust-b53454e2e413ac58da20933968cb4a86a3c7c476.tar.gz rust-b53454e2e413ac58da20933968cb4a86a3c7c476.zip | |
Move std::{reflect,repr,Poly} to a libdebug crate
This commit moves reflection (as well as the {:?} format modifier) to a new
libdebug crate, all of which is marked experimental.
This is a breaking change because it now requires the debug crate to be
explicitly linked if the :? format qualifier is used. This means that any code
using this feature will have to add `extern crate debug;` to the top of the
crate. Any code relying on reflection will also need to do this.
Closes #12019
[breaking-change]
Diffstat (limited to 'src/libdebug')
| -rw-r--r-- | src/libdebug/fmt.rs | 53 | ||||
| -rw-r--r-- | src/libdebug/lib.rs | 32 | ||||
| -rw-r--r-- | src/libdebug/reflect.rs | 438 | ||||
| -rw-r--r-- | src/libdebug/repr.rs | 659 |
4 files changed, 1182 insertions, 0 deletions
diff --git a/src/libdebug/fmt.rs b/src/libdebug/fmt.rs new file mode 100644 index 00000000000..4087cb95271 --- /dev/null +++ b/src/libdebug/fmt.rs @@ -0,0 +1,53 @@ +// Copyright 2014 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. + +//! Implementation of the `{:?}` format qualifier +//! +//! This module contains the `Poly` trait which is used to implement the `{:?}` +//! format expression in formatting macros. This trait is defined for all types +//! automatically, so it is likely not necessary to use this module manually + +use std::fmt; + +use repr; + +/// Format trait for the `?` character +pub trait Poly { + /// Formats the value using the given formatter. + #[experimental] + fn fmt(&self, &mut fmt::Formatter) -> fmt::Result; +} + +#[doc(hidden)] +pub fn secret_poly<T: Poly>(x: &T, fmt: &mut fmt::Formatter) -> fmt::Result { + // FIXME #11938 - UFCS would make us able call the this method + // directly Poly::fmt(x, fmt). + x.fmt(fmt) +} + +impl<T> Poly for T { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match (f.width, f.precision) { + (None, None) => { + match repr::write_repr(f, self) { + Ok(()) => Ok(()), + Err(..) => Err(fmt::WriteError), + } + } + + // If we have a specified width for formatting, then we have to make + // this allocation of a new string + _ => { + let s = repr::repr_to_str(self); + f.pad(s.as_slice()) + } + } + } +} diff --git a/src/libdebug/lib.rs b/src/libdebug/lib.rs new file mode 100644 index 00000000000..452c3d2937c --- /dev/null +++ b/src/libdebug/lib.rs @@ -0,0 +1,32 @@ +// Copyright 2014 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. + +//! Debugging utilities for Rust programs +//! +//! This crate is intended to provide useful functionality when debugging +//! programs, such as reflection for printing values. This crate is currently +//! entirely experimental as its makeup will likely change over time. +//! Additionally, it is not guaranteed that functionality such as reflection +//! will persist into the future. + +#![crate_id = "debug#0.11.0-pre"] +#![license = "MIT/ASL2"] +#![crate_type = "rlib"] +#![crate_type = "dylib"] +#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", + html_favicon_url = "http://www.rust-lang.org/favicon.ico", + html_root_url = "http://doc.rust-lang.org/")] +#![experimental] +#![feature(managed_boxes, macro_rules, quad_precision_float)] +#![allow(experimental)] + +pub mod fmt; +pub mod reflect; +pub mod repr; diff --git a/src/libdebug/reflect.rs b/src/libdebug/reflect.rs new file mode 100644 index 00000000000..367994466b2 --- /dev/null +++ b/src/libdebug/reflect.rs @@ -0,0 +1,438 @@ +// Copyright 2012 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. + +/*! + +Runtime type reflection + +*/ + +#![allow(missing_doc)] + +use std::intrinsics::{Disr, Opaque, TyDesc, TyVisitor}; +use std::mem; + +/** + * 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. + */ +pub trait MovePtr { + fn move_ptr(&mut self, adjustment: |*u8| -> *u8); + fn push_ptr(&mut self); + fn pop_ptr(&mut self); +} + +/// Helper function for alignment calculation. +#[inline] +pub fn align(size: uint, align: uint) -> uint { + ((size + align) - 1u) & !(align - 1u) +} + +/// Adaptor to wrap around visitors implementing MovePtr. +pub struct MovePtrAdaptor<V> { + inner: V +} +pub fn MovePtrAdaptor<V:TyVisitor + MovePtr>(v: V) -> MovePtrAdaptor<V> { + MovePtrAdaptor { inner: v } +} + +impl<V:TyVisitor + MovePtr> MovePtrAdaptor<V> { + #[inline] + pub fn bump(&mut self, sz: uint) { + self.inner.move_ptr(|p| ((p as uint) + sz) as *u8) + } + + #[inline] + pub fn align(&mut self, a: uint) { + self.inner.move_ptr(|p| align(p as uint, a) as *u8) + } + + #[inline] + pub fn align_to<T>(&mut self) { + self.align(mem::min_align_of::<T>()); + } + + #[inline] + pub fn bump_past<T>(&mut self) { + self.bump(mem::size_of::<T>()); + } + + pub fn unwrap(self) -> V { self.inner } +} + +/// Abstract type-directed pointer-movement using the MovePtr trait +impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> { + fn visit_bot(&mut self) -> bool { + self.align_to::<()>(); + if ! self.inner.visit_bot() { return false; } + self.bump_past::<()>(); + true + } + + fn visit_nil(&mut self) -> bool { + self.align_to::<()>(); + if ! self.inner.visit_nil() { return false; } + self.bump_past::<()>(); + true + } + + fn visit_bool(&mut self) -> bool { + self.align_to::<bool>(); + if ! self.inner.visit_bool() { return false; } + self.bump_past::<bool>(); + true + } + + fn visit_int(&mut self) -> bool { + self.align_to::<int>(); + if ! self.inner.visit_int() { return false; } + self.bump_past::<int>(); + true + } + + fn visit_i8(&mut self) -> bool { + self.align_to::<i8>(); + if ! self.inner.visit_i8() { return false; } + self.bump_past::<i8>(); + true + } + + fn visit_i16(&mut self) -> bool { + self.align_to::<i16>(); + if ! self.inner.visit_i16() { return false; } + self.bump_past::<i16>(); + true + } + + fn visit_i32(&mut self) -> bool { + self.align_to::<i32>(); + if ! self.inner.visit_i32() { return false; } + self.bump_past::<i32>(); + true + } + + fn visit_i64(&mut self) -> bool { + self.align_to::<i64>(); + if ! self.inner.visit_i64() { return false; } + self.bump_past::<i64>(); + true + } + + fn visit_uint(&mut self) -> bool { + self.align_to::<uint>(); + if ! self.inner.visit_uint() { return false; } + self.bump_past::<uint>(); + true + } + + fn visit_u8(&mut self) -> bool { + self.align_to::<u8>(); + if ! self.inner.visit_u8() { return false; } + self.bump_past::<u8>(); + true + } + + fn visit_u16(&mut self) -> bool { + self.align_to::<u16>(); + if ! self.inner.visit_u16() { return false; } + self.bump_past::<u16>(); + true + } + + fn visit_u32(&mut self) -> bool { + self.align_to::<u32>(); + if ! self.inner.visit_u32() { return false; } + self.bump_past::<u32>(); + true + } + + fn visit_u64(&mut self) -> bool { + self.align_to::<u64>(); + if ! self.inner.visit_u64() { return false; } + self.bump_past::<u64>(); + true + } + + fn visit_f32(&mut self) -> bool { + self.align_to::<f32>(); + if ! self.inner.visit_f32() { return false; } + self.bump_past::<f32>(); + true + } + + fn visit_f64(&mut self) -> bool { + self.align_to::<f64>(); + if ! self.inner.visit_f64() { return false; } + self.bump_past::<f64>(); + true + } + + fn visit_f128(&mut self) -> bool { + self.align_to::<f128>(); + if ! self.inner.visit_f128() { return false; } + self.bump_past::<f128>(); + true + } + + fn visit_char(&mut self) -> bool { + self.align_to::<char>(); + if ! self.inner.visit_char() { return false; } + self.bump_past::<char>(); + true + } + + fn visit_estr_box(&mut self) -> bool { + true + } + + fn visit_estr_uniq(&mut self) -> bool { + self.align_to::<~str>(); + if ! self.inner.visit_estr_uniq() { return false; } + self.bump_past::<~str>(); + true + } + + fn visit_estr_slice(&mut self) -> bool { + self.align_to::<&'static str>(); + if ! self.inner.visit_estr_slice() { return false; } + self.bump_past::<&'static str>(); + true + } + + fn visit_estr_fixed(&mut self, 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(&mut self, 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(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + self.align_to::<Box<u8>>(); + if ! self.inner.visit_uniq(mtbl, inner) { return false; } + self.bump_past::<Box<u8>>(); + true + } + + fn visit_ptr(&mut self, 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(&mut self, 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_evec_box(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { + true + } + + fn visit_evec_uniq(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint, + align: uint) -> bool { + self.align(align); + if ! self.inner.visit_enter_class(name, named_fields, n_fields, sz, align) { + return false; + } + true + } + + fn visit_class_field(&mut self, i: uint, name: &str, named: bool, mtbl: uint, + inner: *TyDesc) -> bool { + unsafe { self.align((*inner).align); } + if ! self.inner.visit_class_field(i, name, named, mtbl, inner) { + return false; + } + unsafe { self.bump((*inner).size); } + true + } + + fn visit_leave_class(&mut self, name: &str, named_fields: bool, n_fields: uint, sz: uint, + align: uint) -> bool { + if ! self.inner.visit_leave_class(name, named_fields, n_fields, sz, align) { + return false; + } + true + } + + fn visit_enter_tup(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, 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(&mut self, i: uint, mode: uint, inner: *TyDesc) -> bool { + if ! self.inner.visit_fn_input(i, mode, inner) { return false; } + true + } + + fn visit_fn_output(&mut self, retstyle: uint, variadic: bool, inner: *TyDesc) -> bool { + if ! self.inner.visit_fn_output(retstyle, variadic, inner) { return false; } + true + } + + fn visit_leave_fn(&mut self, 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(&mut self, n_variants: uint, + get_disr: unsafe extern fn(ptr: *Opaque) -> Disr, + sz: uint, align: uint) + -> bool { + self.align(align); + if ! self.inner.visit_enter_enum(n_variants, get_disr, sz, align) { + return false; + } + true + } + + fn visit_enter_enum_variant(&mut self, variant: uint, + disr_val: Disr, + 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(&mut self, i: uint, offset: uint, inner: *TyDesc) -> bool { + self.inner.push_ptr(); + self.bump(offset); + if ! self.inner.visit_enum_variant_field(i, offset, inner) { return false; } + self.inner.pop_ptr(); + true + } + + fn visit_leave_enum_variant(&mut self, variant: uint, + disr_val: Disr, + 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(&mut self, n_variants: uint, + get_disr: unsafe extern fn(ptr: *Opaque) -> Disr, + sz: uint, align: uint) -> bool { + if ! self.inner.visit_leave_enum(n_variants, get_disr, sz, align) { + return false; + } + self.bump(sz); + true + } + + fn visit_trait(&mut self, name: &str) -> bool { + self.align_to::<Box<TyVisitor>>(); + if ! self.inner.visit_trait(name) { return false; } + self.bump_past::<Box<TyVisitor>>(); + true + } + + fn visit_param(&mut self, i: uint) -> bool { + if ! self.inner.visit_param(i) { return false; } + true + } + + fn visit_self(&mut self) -> bool { + self.align_to::<&'static u8>(); + if ! self.inner.visit_self() { return false; } + self.align_to::<&'static u8>(); + true + } +} diff --git a/src/libdebug/repr.rs b/src/libdebug/repr.rs new file mode 100644 index 00000000000..6807e73982d --- /dev/null +++ b/src/libdebug/repr.rs @@ -0,0 +1,659 @@ +// Copyright 2012 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. + +/*! + +More runtime type reflection + +*/ + +use std::char; +use std::intrinsics::{Disr, Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc}; +use std::io; +use std::mem; +use std::raw; + +use reflect; +use reflect::{MovePtr, align}; + +macro_rules! try( ($me:expr, $e:expr) => ( + match $e { + Ok(()) => {}, + Err(e) => { $me.last_err = Some(e); return false; } + } +) ) + +/// Representations + +trait Repr { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()>; +} + +impl Repr for () { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> { + writer.write("()".as_bytes()) + } +} + +impl Repr for bool { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> { + let s = if *self { "true" } else { "false" }; + writer.write(s.as_bytes()) + } +} + +impl Repr for int { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> { + write!(writer, "{}", *self) + } +} + +macro_rules! int_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> { + write!(writer, "{}{}", *self, $suffix) + } +})) + +int_repr!(i8, "i8") +int_repr!(i16, "i16") +int_repr!(i32, "i32") +int_repr!(i64, "i64") +int_repr!(uint, "u") +int_repr!(u8, "u8") +int_repr!(u16, "u16") +int_repr!(u32, "u32") +int_repr!(u64, "u64") + +macro_rules! num_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty { + fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()> { + let s = self.to_str(); + writer.write(s.as_bytes()).and_then(|()| { + writer.write(bytes!($suffix)) + }) + } +})) + +num_repr!(f32, "f32") +num_repr!(f64, "f64") + +// New implementation using reflect::MovePtr + +enum VariantState { + SearchingFor(Disr), + Matched, + AlreadyFound +} + +pub struct ReprVisitor<'a> { + ptr: *u8, + ptr_stk: Vec<*u8>, + var_stk: Vec<VariantState>, + writer: &'a mut io::Writer, + last_err: Option<io::IoError>, +} + +pub fn ReprVisitor<'a>(ptr: *u8, + writer: &'a mut io::Writer) -> ReprVisitor<'a> { + ReprVisitor { + ptr: ptr, + ptr_stk: vec!(), + var_stk: vec!(), + writer: writer, + last_err: None, + } +} + +impl<'a> MovePtr for ReprVisitor<'a> { + #[inline] + fn move_ptr(&mut self, adjustment: |*u8| -> *u8) { + self.ptr = adjustment(self.ptr); + } + fn push_ptr(&mut self) { + self.ptr_stk.push(self.ptr); + } + fn pop_ptr(&mut self) { + self.ptr = self.ptr_stk.pop().unwrap(); + } +} + +impl<'a> ReprVisitor<'a> { + // Various helpers for the TyVisitor impl + + #[inline] + pub fn get<T>(&mut self, f: |&mut ReprVisitor, &T| -> bool) -> bool { + unsafe { + f(self, mem::transmute::<*u8,&T>(self.ptr)) + } + } + + #[inline] + pub fn visit_inner(&mut self, inner: *TyDesc) -> bool { + self.visit_ptr_inner(self.ptr, inner) + } + + #[inline] + pub fn visit_ptr_inner(&mut self, ptr: *u8, inner: *TyDesc) -> bool { + unsafe { + // This should call the constructor up above, but due to limiting + // issues we have to recreate it here. + let u = ReprVisitor { + ptr: ptr, + ptr_stk: vec!(), + var_stk: vec!(), + writer: mem::transmute_copy(&self.writer), + last_err: None, + }; + let mut v = reflect::MovePtrAdaptor(u); + // Obviously this should not be a thing, but blame #8401 for now + visit_tydesc(inner, &mut v as &mut TyVisitor); + match v.unwrap().last_err { + Some(e) => { + self.last_err = Some(e); + false + } + None => true, + } + } + } + + #[inline] + pub fn write<T:Repr>(&mut self) -> bool { + self.get(|this, v:&T| { + try!(this, v.write_repr(this.writer)); + true + }) + } + + pub fn write_escaped_slice(&mut self, slice: &str) -> bool { + try!(self, self.writer.write(['"' as u8])); + for ch in slice.chars() { + if !self.write_escaped_char(ch, true) { return false } + } + try!(self, self.writer.write(['"' as u8])); + true + } + + pub fn write_mut_qualifier(&mut self, mtbl: uint) -> bool { + if mtbl == 0 { + try!(self, self.writer.write("mut ".as_bytes())); + } else if mtbl == 1 { + // skip, this is ast::m_imm + } else { + fail!("invalid mutability value"); + } + true + } + + pub fn write_vec_range(&mut self, ptr: *(), len: uint, inner: *TyDesc) -> bool { + let mut p = ptr as *u8; + let (sz, al) = unsafe { ((*inner).size, (*inner).align) }; + try!(self, self.writer.write(['[' as u8])); + let mut first = true; + let mut left = len; + // unit structs have 0 size, and don't loop forever. + let dec = if sz == 0 {1} else {sz}; + while left > 0 { + if first { + first = false; + } else { + try!(self, self.writer.write(", ".as_bytes())); + } + self.visit_ptr_inner(p as *u8, inner); + p = align(unsafe { p.offset(sz as int) as uint }, al) as *u8; + left -= dec; + } + try!(self, self.writer.write([']' as u8])); + true + } + + pub fn write_unboxed_vec_repr(&mut self, _: uint, v: &raw::Vec<()>, inner: *TyDesc) -> bool { + self.write_vec_range(&v.data, v.fill, inner) + } + + fn write_escaped_char(&mut self, ch: char, is_str: bool) -> bool { + try!(self, match ch { + '\t' => self.writer.write("\\t".as_bytes()), + '\r' => self.writer.write("\\r".as_bytes()), + '\n' => self.writer.write("\\n".as_bytes()), + '\\' => self.writer.write("\\\\".as_bytes()), + '\'' => { + if is_str { + self.writer.write("'".as_bytes()) + } else { + self.writer.write("\\'".as_bytes()) + } + } + '"' => { + if is_str { + self.writer.write("\\\"".as_bytes()) + } else { + self.writer.write("\"".as_bytes()) + } + } + '\x20'..'\x7e' => self.writer.write([ch as u8]), + _ => { + char::escape_unicode(ch, |c| { + let _ = self.writer.write([c as u8]); + }); + Ok(()) + } + }); + return true; + } +} + +impl<'a> TyVisitor for ReprVisitor<'a> { + fn visit_bot(&mut self) -> bool { + try!(self, self.writer.write("!".as_bytes())); + true + } + fn visit_nil(&mut self) -> bool { self.write::<()>() } + fn visit_bool(&mut self) -> bool { self.write::<bool>() } + fn visit_int(&mut self) -> bool { self.write::<int>() } + fn visit_i8(&mut self) -> bool { self.write::<i8>() } + fn visit_i16(&mut self) -> bool { self.write::<i16>() } + fn visit_i32(&mut self) -> bool { self.write::<i32>() } + fn visit_i64(&mut self) -> bool { self.write::<i64>() } + + fn visit_uint(&mut self) -> bool { self.write::<uint>() } + fn visit_u8(&mut self) -> bool { self.write::<u8>() } + fn visit_u16(&mut self) -> bool { self.write::<u16>() } + fn visit_u32(&mut self) -> bool { self.write::<u32>() } + fn visit_u64(&mut self) -> bool { self.write::<u64>() } + + fn visit_f32(&mut self) -> bool { self.write::<f32>() } + fn visit_f64(&mut self) -> bool { self.write::<f64>() } + fn visit_f128(&mut self) -> bool { fail!("not implemented") } + + fn visit_char(&mut self) -> bool { + self.get::<char>(|this, &ch| { + try!(this, this.writer.write(['\'' as u8])); + if !this.write_escaped_char(ch, false) { return false } + try!(this, this.writer.write(['\'' as u8])); + true + }) + } + + fn visit_estr_box(&mut self) -> bool { + true + } + + fn visit_estr_uniq(&mut self) -> bool { + self.get::<~str>(|this, s| { + try!(this, this.writer.write(['~' as u8])); + this.write_escaped_slice(*s) + }) + } + + fn visit_estr_slice(&mut self) -> bool { + self.get::<&str>(|this, s| this.write_escaped_slice(*s)) + } + + // Type no longer exists, vestigial function. + fn visit_estr_fixed(&mut self, _n: uint, _sz: uint, + _align: uint) -> bool { fail!(); } + + fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + try!(self, self.writer.write(['@' as u8])); + self.write_mut_qualifier(mtbl); + self.get::<&raw::Box<()>>(|this, b| { + let p = &b.data as *() as *u8; + this.visit_ptr_inner(p, inner) + }) + } + + fn visit_uniq(&mut self, _mtbl: uint, inner: *TyDesc) -> bool { + try!(self, self.writer.write("box ".as_bytes())); + self.get::<*u8>(|this, b| { + this.visit_ptr_inner(*b, inner) + }) + } + + fn visit_ptr(&mut self, mtbl: uint, _inner: *TyDesc) -> bool { + self.get::<*u8>(|this, p| { + try!(this, write!(this.writer, "({} as *", *p)); + this.write_mut_qualifier(mtbl); + try!(this, this.writer.write("())".as_bytes())); + true + }) + } + + fn visit_rptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + try!(self, self.writer.write(['&' as u8])); + self.write_mut_qualifier(mtbl); + self.get::<*u8>(|this, p| { + this.visit_ptr_inner(*p, inner) + }) + } + + fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + self.get::<&raw::Box<raw::Vec<()>>>(|this, b| { + try!(this, this.writer.write(['@' as u8])); + this.write_mut_qualifier(mtbl); + this.write_unboxed_vec_repr(mtbl, &b.data, inner) + }) + } + + fn visit_evec_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + self.get::<&raw::Vec<()>>(|this, b| { + try!(this, this.writer.write("box ".as_bytes())); + this.write_unboxed_vec_repr(mtbl, *b, inner) + }) + } + + fn visit_evec_slice(&mut self, mtbl: uint, inner: *TyDesc) -> bool { + self.get::<raw::Slice<()>>(|this, s| { + try!(this, this.writer.write(['&' as u8])); + this.write_mut_qualifier(mtbl); + let size = unsafe { + if (*inner).size == 0 { 1 } else { (*inner).size } + }; + this.write_vec_range(s.data, s.len * size, inner) + }) + } + + fn visit_evec_fixed(&mut self, n: uint, sz: uint, _align: uint, + _: uint, inner: *TyDesc) -> bool { + let assumed_size = if sz == 0 { n } else { sz }; + self.get::<()>(|this, b| { + this.write_vec_range(b, assumed_size, inner) + }) + } + + fn visit_enter_rec(&mut self, _n_fields: uint, + _sz: uint, _align: uint) -> bool { + try!(self, self.writer.write(['{' as u8])); + true + } + + fn visit_rec_field(&mut self, i: uint, name: &str, + mtbl: uint, inner: *TyDesc) -> bool { + if i != 0 { + try!(self, self.writer.write(", ".as_bytes())); + } + self.write_mut_qualifier(mtbl); + try!(self, self.writer.write(name.as_bytes())); + try!(self, self.writer.write(": ".as_bytes())); + self.visit_inner(inner); + true + } + + fn visit_leave_rec(&mut self, _n_fields: uint, + _sz: uint, _align: uint) -> bool { + try!(self, self.writer.write(['}' as u8])); + true + } + + fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint, + _sz: uint, _align: uint) -> bool { + try!(self, self.writer.write(name.as_bytes())); + if n_fields != 0 { + if named_fields { + try!(self, self.writer.write(['{' as u8])); + } else { + try!(self, self.writer.write(['(' as u8])); + } + } + true + } + + fn visit_class_field(&mut self, i: uint, name: &str, named: bool, + _mtbl: uint, inner: *TyDesc) -> bool { + if i != 0 { + try!(self, self.writer.write(", ".as_bytes())); + } + if named { + try!(self, self.writer.write(name.as_bytes())); + try!(self, self.writer.write(": ".as_bytes())); + } + self.visit_inner(inner); + true + } + + fn visit_leave_class(&mut self, _name: &str, named_fields: bool, n_fields: uint, + _sz: uint, _align: uint) -> bool { + if n_fields != 0 { + if named_fields { + try!(self, self.writer.write(['}' as u8])); + } else { + try!(self, self.writer.write([')' as u8])); + } + } + true + } + + fn visit_enter_tup(&mut self, _n_fields: uint, + _sz: uint, _align: uint) -> bool { + try!(self, self.writer.write(['(' as u8])); + true + } + + fn visit_tup_field(&mut self, i: uint, inner: *TyDesc) -> bool { + if i != 0 { + try!(self, self.writer.write(", ".as_bytes())); + } + self.visit_inner(inner); + true + } + + fn visit_leave_tup(&mut self, _n_fields: uint, + _sz: uint, _align: uint) -> bool { + if _n_fields == 1 { + try!(self, self.writer.write([',' as u8])); + } + try!(self, self.writer.write([')' as u8])); + true + } + + fn visit_enter_enum(&mut self, + _n_variants: uint, + get_disr: unsafe extern fn(ptr: *Opaque) -> Disr, + _sz: uint, + _align: uint) -> bool { + let disr = unsafe { + get_disr(mem::transmute(self.ptr)) + }; + self.var_stk.push(SearchingFor(disr)); + true + } + + fn visit_enter_enum_variant(&mut self, _variant: uint, + disr_val: Disr, + n_fields: uint, + name: &str) -> bool { + let mut write = false; + match self.var_stk.pop().unwrap() { + SearchingFor(sought) => { + if disr_val == sought { + self.var_stk.push(Matched); + write = true; + } else { + self.var_stk.push(SearchingFor(sought)); + } + } + Matched | AlreadyFound => { + self.var_stk.push(AlreadyFound); + } + } + + if write { + try!(self, self.writer.write(name.as_bytes())); + if n_fields > 0 { + try!(self, self.writer.write(['(' as u8])); + } + } + true + } + + fn visit_enum_variant_field(&mut self, + i: uint, + _offset: uint, + inner: *TyDesc) + -> bool { + match *self.var_stk.get(self.var_stk.len() - 1) { + Matched => { + if i != 0 { + try!(self, self.writer.write(", ".as_bytes())); + } + if ! self.visit_inner(inner) { + return false; + } + } + _ => () + } + true + } + + fn visit_leave_enum_variant(&mut self, _variant: uint, + _disr_val: Disr, + n_fields: uint, + _name: &str) -> bool { + match *self.var_stk.get(self.var_stk.len() - 1) { + Matched => { + if n_fields > 0 { + try!(self, self.writer.write([')' as u8])); + } + } + _ => () + } + true + } + + fn visit_leave_enum(&mut self, + _n_variants: uint, + _get_disr: unsafe extern fn(ptr: *Opaque) -> Disr, + _sz: uint, + _align: uint) + -> bool { + match self.var_stk.pop().unwrap() { + SearchingFor(..) => fail!("enum value matched no variant"), + _ => true + } + } + + fn visit_enter_fn(&mut self, _purity: uint, _proto: uint, + _n_inputs: uint, _retstyle: uint) -> bool { + try!(self, self.writer.write("fn(".as_bytes())); + true + } + + fn visit_fn_input(&mut self, i: uint, _mode: uint, inner: *TyDesc) -> bool { + if i != 0 { + try!(self, self.writer.write(", ".as_bytes())); + } + let name = unsafe { (*inner).name }; + try!(self, self.writer.write(name.as_bytes())); + true + } + + fn visit_fn_output(&mut self, _retstyle: uint, variadic: bool, + inner: *TyDesc) -> bool { + if variadic { + try!(self, self.writer.write(", ...".as_bytes())); + } + try!(self, self.writer.write(")".as_bytes())); + let name = unsafe { (*inner).name }; + if name != "()" { + try!(self, self.writer.write(" -> ".as_bytes())); + try!(self, self.writer.write(name.as_bytes())); + } + true + } + + fn visit_leave_fn(&mut self, _purity: uint, _proto: uint, + _n_inputs: uint, _retstyle: uint) -> bool { true } + + + fn visit_trait(&mut self, name: &str) -> bool { + try!(self, self.writer.write(name.as_bytes())); + true + } + + fn visit_param(&mut self, _i: uint) -> bool { true } + fn visit_self(&mut self) -> bool { true } +} + +pub fn write_repr<T>(writer: &mut io::Writer, object: &T) -> io::IoResult<()> { + unsafe { + let ptr = object as *T as *u8; + let tydesc = get_tydesc::<T>(); + let u = ReprVisitor(ptr, writer); + let mut v = reflect::MovePtrAdaptor(u); + visit_tydesc(tydesc, &mut v as &mut TyVisitor); + match v.unwrap().last_err { + Some(e) => Err(e), + None => Ok(()), + } + } +} + +pub fn repr_to_str<T>(t: &T) -> String { + let mut result = io::MemWriter::new(); + write_repr(&mut result as &mut io::Writer, t).unwrap(); + String::from_utf8(result.unwrap()).unwrap() +} + +#[cfg(test)] +struct P {a: int, b: f64} + +#[test] +fn test_repr() { + use std::str; + use std::io::stdio::println; + use std::char::is_alphabetic; + use std::mem::swap; + + fn exact_test<T>(t: &T, e:&str) { + let mut m = io::MemWriter::new(); + write_repr(&mut m as &mut io::Writer, t).unwrap(); + let s = str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned(); + assert_eq!(s.as_slice(), e); + } + + exact_test(&10, "10"); + exact_test(&true, "true"); + exact_test(&false, "false"); + exact_test(&1.234, "1.234f64"); + exact_test(&("hello"), "\"hello\""); + + exact_test(&(@10), "@10"); + exact_test(&(box 10), "box 10"); + exact_test(&(&10), "&10"); + let mut x = 10; + exact_test(&(&mut x), "&mut 10"); + + exact_test(&(0 as *()), "(0x0 as *())"); + exact_test(&(0 as *mut ()), "(0x0 as *mut ())"); + + exact_test(&(1,), "(1,)"); + exact_test(&(&["hi", "there"]), + "&[\"hi\", \"there\"]"); + exact_test(&(P{a:10, b:1.234}), + "repr::P{a: 10, b: 1.234f64}"); + exact_test(&(@P{a:10, b:1.234}), + "@repr::P{a: 10, b: 1.234f64}"); + exact_test(&(box P{a:10, b:1.234}), + "box repr::P{a: 10, b: 1.234f64}"); + + exact_test(&(&[1, 2]), "&[1, 2]"); + exact_test(&(&mut [1, 2]), "&mut [1, 2]"); + + exact_test(&'\'', "'\\''"); + exact_test(&'"', "'\"'"); + exact_test(&("'"), "\"'\""); + exact_test(&("\""), "\"\\\"\""); + + exact_test(&println, "fn(&str)"); + exact_test(&swap::<int>, "fn(&mut int, &mut int)"); + exact_test(&is_alphabetic, "fn(char) -> bool"); + + struct Bar(int, int); + exact_test(&(Bar(2, 2)), "repr::test_repr::Bar(2, 2)"); +} |
