Demode extfmt

Needs a snapshot before this can be completed, because I changed
the mode for conv_poly.
This commit is contained in:
Tim Chevalier 2012-09-28 12:22:33 -07:00
parent 21519bc7e0
commit 2f4ee89119
10 changed files with 117 additions and 105 deletions

View File

@ -21,7 +21,7 @@ pub unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
* reinterpret_cast on managed pointer types.
*/
#[inline(always)]
pub unsafe fn forget<T>(-thing: T) { rusti::forget(move thing); }
pub unsafe fn forget<T>(+thing: T) { rusti::forget(move thing); }
/**
* Force-increment the reference count on a shared box. If used
@ -40,7 +40,7 @@ pub unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
* assert transmute("L") == ~[76u8, 0u8];
*/
#[inline(always)]
pub unsafe fn transmute<L, G>(-thing: L) -> G {
pub unsafe fn transmute<L, G>(+thing: L) -> G {
let newthing: G = reinterpret_cast(&thing);
forget(move thing);
move newthing

View File

@ -81,14 +81,14 @@ mod ct {
// A fragment of the output sequence
enum Piece { PieceString(~str), PieceConv(Conv), }
type ErrorFn = fn@(~str) -> ! ;
type ErrorFn = fn@(&str) -> ! ;
fn parse_fmt_string(s: ~str, error: ErrorFn) -> ~[Piece] {
fn parse_fmt_string(s: &str, error: ErrorFn) -> ~[Piece] {
let mut pieces: ~[Piece] = ~[];
let lim = str::len(s);
let mut buf = ~"";
fn flush_buf(+buf: ~str, &pieces: ~[Piece]) -> ~str {
if str::len(buf) > 0 {
fn flush_buf(+buf: ~str, pieces: &mut ~[Piece]) -> ~str {
if buf.len() > 0 {
let piece = PieceString(move buf);
pieces.push(move piece);
}
@ -108,17 +108,17 @@ mod ct {
buf += curr2;
i += 1;
} else {
buf = flush_buf(move buf, pieces);
buf = flush_buf(move buf, &mut pieces);
let rs = parse_conversion(s, i, lim, error);
pieces.push(copy rs.piece);
i = rs.next;
}
} else { buf += curr; i += size; }
}
flush_buf(move buf, pieces);
flush_buf(move buf, &mut pieces);
move pieces
}
fn peek_num(s: ~str, i: uint, lim: uint) ->
fn peek_num(s: &str, i: uint, lim: uint) ->
Option<{num: uint, next: uint}> {
let mut j = i;
let mut accum = 0u;
@ -140,7 +140,7 @@ mod ct {
None
}
}
fn parse_conversion(s: ~str, i: uint, lim: uint, error: ErrorFn) ->
fn parse_conversion(s: &str, i: uint, lim: uint, error: ErrorFn) ->
{piece: Piece, next: uint} {
let parm = parse_parameter(s, i, lim);
let flags = parse_flags(s, parm.next, lim);
@ -155,7 +155,7 @@ mod ct {
ty: ty.ty}),
next: ty.next};
}
fn parse_parameter(s: ~str, i: uint, lim: uint) ->
fn parse_parameter(s: &str, i: uint, lim: uint) ->
{param: Option<int>, next: uint} {
if i >= lim { return {param: None, next: i}; }
let num = peek_num(s, i, lim);
@ -170,12 +170,12 @@ mod ct {
}
};
}
fn parse_flags(s: ~str, i: uint, lim: uint) ->
fn parse_flags(s: &str, i: uint, lim: uint) ->
{flags: ~[Flag], next: uint} {
let noflags: ~[Flag] = ~[];
if i >= lim { return {flags: move noflags, next: i}; }
fn more_(f: Flag, s: ~str, i: uint, lim: uint) ->
fn more(f: Flag, s: &str, i: uint, lim: uint) ->
{flags: ~[Flag], next: uint} {
let next = parse_flags(s, i + 1u, lim);
let rest = copy next.flags;
@ -183,21 +183,22 @@ mod ct {
let curr: ~[Flag] = ~[f];
return {flags: vec::append(move curr, rest), next: j};
}
let more = |x, copy s| more_(x, copy s, i, lim);
// Unfortunate, but because s is borrowed, can't use a closure
// fn more(f: Flag, s: &str) { more_(f, s, i, lim); }
let f = s[i];
return if f == '-' as u8 {
more(FlagLeftJustify)
more(FlagLeftJustify, s, i, lim)
} else if f == '0' as u8 {
more(FlagLeftZeroPad)
more(FlagLeftZeroPad, s, i, lim)
} else if f == ' ' as u8 {
more(FlagSpaceForSign)
more(FlagSpaceForSign, s, i, lim)
} else if f == '+' as u8 {
more(FlagSignAlways)
more(FlagSignAlways, s, i, lim)
} else if f == '#' as u8 {
more(FlagAlternate)
more(FlagAlternate, s, i, lim)
} else { {flags: move noflags, next: i} };
}
fn parse_count(s: ~str, i: uint, lim: uint)
fn parse_count(s: &str, i: uint, lim: uint)
-> {count: Count, next: uint} {
return if i >= lim {
{count: CountImplied, next: i}
@ -219,7 +220,7 @@ mod ct {
}
};
}
fn parse_precision(s: ~str, i: uint, lim: uint) ->
fn parse_precision(s: &str, i: uint, lim: uint) ->
{count: Count, next: uint} {
return if i >= lim {
{count: CountImplied, next: i}
@ -235,7 +236,7 @@ mod ct {
}
} else { {count: CountImplied, next: i} };
}
fn parse_type(s: ~str, i: uint, lim: uint, error: ErrorFn) ->
fn parse_type(s: &str, i: uint, lim: uint, error: ErrorFn) ->
{ty: Ty, next: uint} {
if i >= lim { error(~"missing type in conversion"); }
let tstr = str::slice(s, i, i+1u);
@ -269,10 +270,7 @@ mod ct {
}
}
// Functions used by the fmt extension at runtime. For now there are a lot of
// decisions made a runtime. If it proves worthwhile then some of these
// conditions can be evaluated at compile-time. For now though it's cleaner to
// implement it 0this way, I think.
// OLD CODE -- eventually remove
mod rt {
#[legacy_exports];
const flag_none : u32 = 0u32;
@ -328,7 +326,7 @@ mod rt {
let mut unpadded = match cv.precision {
CountImplied => s.to_unique(),
CountIs(max) => if max as uint < str::char_len(s) {
str::substr(s, 0u, max as uint)
str::substr(s, 0, max as uint)
} else {
s.to_unique()
}
@ -338,7 +336,7 @@ mod rt {
pure fn conv_float(cv: Conv, f: float) -> ~str {
let (to_str, digits) = match cv.precision {
CountIs(c) => (float::to_str_exact, c as uint),
CountImplied => (float::to_str, 6u)
CountImplied => (float::to_str, 6)
};
let mut s = unsafe { to_str(f, digits) };
if 0.0 <= f {
@ -404,16 +402,17 @@ mod rt {
pure fn ne(other: &PadMode) -> bool { !self.eq(other) }
}
fn pad(cv: Conv, &s: ~str, mode: PadMode) -> ~str {
fn pad(cv: Conv, +s: ~str, mode: PadMode) -> ~str {
let mut s = move s; // sadtimes
let uwidth : uint = match cv.width {
CountImplied => return copy s,
CountImplied => return s,
CountIs(width) => {
// FIXME: width should probably be uint (see Issue #1996)
width as uint
}
};
let strlen = str::char_len(s);
if uwidth <= strlen { return copy s; }
if uwidth <= strlen { return s; }
let mut padchar = ' ';
let diff = uwidth - strlen;
if have_flag(cv.flags, flag_left_justify) {
@ -444,7 +443,7 @@ mod rt {
// zeros. It may make sense to convert zero padding to a precision
// instead.
if signed && zero_padding && str::len(s) > 0u {
if signed && zero_padding && s.len() > 0 {
let head = str::shift_char(&mut s);
if head == '+' || head == '-' || head == ' ' {
let headstr = str::from_chars(vec::from_elem(1u, head));
@ -461,7 +460,12 @@ mod rt {
}
}
// XXX remove after snapshots
// NEW CODE
// Functions used by the fmt extension at runtime. For now there are a lot of
// decisions made a runtime. If it proves worthwhile then some of these
// conditions can be evaluated at compile-time. For now though it's cleaner to
// implement it 0this way, I think.
mod rt2 {
#[legacy_exports];
const flag_none : u32 = 0u32;
@ -477,7 +481,7 @@ mod rt2 {
type Conv = {flags: u32, width: Count, precision: Count, ty: Ty};
pure fn conv_int(cv: Conv, i: int) -> ~str {
let radix = 10u;
let radix = 10;
let prec = get_int_precision(cv);
let mut s : ~str = int_to_str_prec(i, radix, prec);
if 0 <= i {
@ -511,7 +515,7 @@ mod rt2 {
let mut s = str::from_char(c);
return unsafe { pad(cv, s, PadNozero) };
}
pure fn conv_str(cv: Conv, s: &str) -> ~str {
pure fn conv_str(cv: Conv, +s: &str) -> ~str {
// For strings, precision is the maximum characters
// displayed
let mut unpadded = match cv.precision {
@ -539,8 +543,8 @@ mod rt2 {
}
return unsafe { pad(cv, s, PadFloat) };
}
pure fn conv_poly<T>(cv: Conv, v: T) -> ~str {
let s = sys::log_str(&v);
pure fn conv_poly<T>(cv: Conv, v: &T) -> ~str {
let s = sys::log_str(v);
return conv_str(cv, s);
}
@ -593,16 +597,17 @@ mod rt2 {
pure fn ne(other: &PadMode) -> bool { !self.eq(other) }
}
fn pad(cv: Conv, &s: ~str, mode: PadMode) -> ~str {
fn pad(cv: Conv, +s: ~str, mode: PadMode) -> ~str {
let mut s = move s; // sadtimes
let uwidth : uint = match cv.width {
CountImplied => return copy s,
CountImplied => return s,
CountIs(width) => {
// FIXME: width should probably be uint (see Issue #1996)
width as uint
}
};
let strlen = str::char_len(s);
if uwidth <= strlen { return copy s; }
if uwidth <= strlen { return s; }
let mut padchar = ' ';
let diff = uwidth - strlen;
if have_flag(cv.flags, flag_left_justify) {
@ -633,7 +638,7 @@ mod rt2 {
// zeros. It may make sense to convert zero padding to a precision
// instead.
if signed && zero_padding && str::len(s) > 0u {
if signed && zero_padding && s.len() > 0 {
let head = str::shift_char(&mut s);
if head == '+' || head == '-' || head == ' ' {
let headstr = str::from_chars(vec::from_elem(1u, head));

View File

@ -328,7 +328,7 @@ pub fn program_output(prog: &str, args: &[~str]) ->
return {status: status, out: move outs, err: move errs};
}
fn writeclose(fd: c_int, s: &str) {
fn writeclose(fd: c_int, +s: ~str) {
use io::WriterUtil;
error!("writeclose %d, %s", fd as int, s);

View File

@ -156,14 +156,14 @@ mod v4 {
fn parse_to_ipv4_rep(ip: &str) -> result::Result<Ipv4Rep, ~str> {
let parts = vec::map(str::split_char(ip, '.'), |s| {
match uint::from_str(*s) {
Some(n) if n <= 255u => n,
_ => 256u
Some(n) if n <= 255 => n,
_ => 256
}
});
if vec::len(parts) != 4u {
if parts.len() != 4 {
result::Err(fmt!("'%s' doesn't have 4 parts", ip))
}
else if vec::contains(parts, &256u) {
else if parts.contains(&256) {
result::Err(fmt!("invalid octal in addr '%s'", ip))
}
else {

View File

@ -10,30 +10,30 @@ export ice_msg;
export expect;
type emitter = fn@(cmsp: Option<(codemap::codemap, span)>,
msg: ~str, lvl: level);
msg: &str, lvl: level);
trait span_handler {
fn span_fatal(sp: span, msg: ~str) -> !;
fn span_err(sp: span, msg: ~str);
fn span_warn(sp: span, msg: ~str);
fn span_note(sp: span, msg: ~str);
fn span_bug(sp: span, msg: ~str) -> !;
fn span_unimpl(sp: span, msg: ~str) -> !;
fn span_fatal(sp: span, msg: &str) -> !;
fn span_err(sp: span, msg: &str);
fn span_warn(sp: span, msg: &str);
fn span_note(sp: span, msg: &str);
fn span_bug(sp: span, msg: &str) -> !;
fn span_unimpl(sp: span, msg: &str) -> !;
fn handler() -> handler;
}
trait handler {
fn fatal(msg: ~str) -> !;
fn err(msg: ~str);
fn fatal(msg: &str) -> !;
fn err(msg: &str);
fn bump_err_count();
fn has_errors() -> bool;
fn abort_if_errors();
fn warn(msg: ~str);
fn note(msg: ~str);
fn bug(msg: ~str) -> !;
fn unimpl(msg: ~str) -> !;
fn emit(cmsp: Option<(codemap::codemap, span)>, msg: ~str, lvl: level);
fn warn(msg: &str);
fn note(msg: &str);
fn bug(msg: &str) -> !;
fn unimpl(msg: &str) -> !;
fn emit(cmsp: Option<(codemap::codemap, span)>, msg: &str, lvl: level);
}
type handler_t = @{
@ -47,24 +47,24 @@ type codemap_t = @{
};
impl codemap_t: span_handler {
fn span_fatal(sp: span, msg: ~str) -> ! {
fn span_fatal(sp: span, msg: &str) -> ! {
self.handler.emit(Some((self.cm, sp)), msg, fatal);
fail;
}
fn span_err(sp: span, msg: ~str) {
fn span_err(sp: span, msg: &str) {
self.handler.emit(Some((self.cm, sp)), msg, error);
self.handler.bump_err_count();
}
fn span_warn(sp: span, msg: ~str) {
fn span_warn(sp: span, msg: &str) {
self.handler.emit(Some((self.cm, sp)), msg, warning);
}
fn span_note(sp: span, msg: ~str) {
fn span_note(sp: span, msg: &str) {
self.handler.emit(Some((self.cm, sp)), msg, note);
}
fn span_bug(sp: span, msg: ~str) -> ! {
fn span_bug(sp: span, msg: &str) -> ! {
self.span_fatal(sp, ice_msg(msg));
}
fn span_unimpl(sp: span, msg: ~str) -> ! {
fn span_unimpl(sp: span, msg: &str) -> ! {
self.span_bug(sp, ~"unimplemented " + msg);
}
fn handler() -> handler {
@ -73,11 +73,11 @@ impl codemap_t: span_handler {
}
impl handler_t: handler {
fn fatal(msg: ~str) -> ! {
fn fatal(msg: &str) -> ! {
self.emit(None, msg, fatal);
fail;
}
fn err(msg: ~str) {
fn err(msg: &str) {
self.emit(None, msg, error);
self.bump_err_count();
}
@ -97,22 +97,22 @@ impl handler_t: handler {
}
self.fatal(s);
}
fn warn(msg: ~str) {
fn warn(msg: &str) {
self.emit(None, msg, warning);
}
fn note(msg: ~str) {
fn note(msg: &str) {
self.emit(None, msg, note);
}
fn bug(msg: ~str) -> ! {
fn bug(msg: &str) -> ! {
self.fatal(ice_msg(msg));
}
fn unimpl(msg: ~str) -> ! { self.bug(~"unimplemented " + msg); }
fn emit(cmsp: Option<(codemap::codemap, span)>, msg: ~str, lvl: level) {
fn unimpl(msg: &str) -> ! { self.bug(~"unimplemented " + msg); }
fn emit(cmsp: Option<(codemap::codemap, span)>, msg: &str, lvl: level) {
self.emit(cmsp, msg, lvl);
}
}
fn ice_msg(msg: ~str) -> ~str {
fn ice_msg(msg: &str) -> ~str {
fmt!("internal compiler error: %s", msg)
}
@ -126,17 +126,19 @@ fn mk_handler(emitter: Option<emitter>) -> handler {
Some(e) => e,
None => {
let f = fn@(cmsp: Option<(codemap::codemap, span)>,
msg: ~str, t: level) {
msg: &str, t: level) {
emit(cmsp, msg, t);
};
f
}
};
@{
mut err_count: 0u,
let x: handler_t = @{
mut err_count: 0,
emit: emit
} as handler
};
x as handler
}
enum level {
@ -171,7 +173,7 @@ fn diagnosticcolor(lvl: level) -> u8 {
}
}
fn print_diagnostic(topic: ~str, lvl: level, msg: ~str) {
fn print_diagnostic(topic: ~str, lvl: level, msg: &str) {
let use_color = term::color_supported() &&
io::stderr().get_type() == io::Screen;
if str::is_not_empty(topic) {
@ -188,7 +190,7 @@ fn print_diagnostic(topic: ~str, lvl: level, msg: ~str) {
}
fn emit(cmsp: Option<(codemap::codemap, span)>,
msg: ~str, lvl: level) {
msg: &str, lvl: level) {
match cmsp {
Some((cm, sp)) => {
let sp = codemap::adjust_span(cm,sp);

View File

@ -133,12 +133,12 @@ trait ext_ctxt {
fn mod_path() -> ~[ast::ident];
fn bt_push(ei: codemap::expn_info_);
fn bt_pop();
fn span_fatal(sp: span, msg: ~str) -> !;
fn span_err(sp: span, msg: ~str);
fn span_warn(sp: span, msg: ~str);
fn span_unimpl(sp: span, msg: ~str) -> !;
fn span_bug(sp: span, msg: ~str) -> !;
fn bug(msg: ~str) -> !;
fn span_fatal(sp: span, msg: &str) -> !;
fn span_err(sp: span, msg: &str);
fn span_warn(sp: span, msg: &str);
fn span_unimpl(sp: span, msg: &str) -> !;
fn span_bug(sp: span, msg: &str) -> !;
fn bug(msg: &str) -> !;
fn next_id() -> ast::node_id;
pure fn trace_macros() -> bool;
fn set_trace_macros(x: bool);
@ -182,27 +182,27 @@ fn mk_ctxt(parse_sess: parse::parse_sess,
_ => self.bug(~"tried to pop without a push")
}
}
fn span_fatal(sp: span, msg: ~str) -> ! {
fn span_fatal(sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_fatal(sp, msg);
}
fn span_err(sp: span, msg: ~str) {
fn span_err(sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_err(sp, msg);
}
fn span_warn(sp: span, msg: ~str) {
fn span_warn(sp: span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_warn(sp, msg);
}
fn span_unimpl(sp: span, msg: ~str) -> ! {
fn span_unimpl(sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_unimpl(sp, msg);
}
fn span_bug(sp: span, msg: ~str) -> ! {
fn span_bug(sp: span, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
fn bug(msg: ~str) -> ! {
fn bug(msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.handler().bug(msg);
}

View File

@ -50,6 +50,10 @@ fn mk_access(cx: ext_ctxt, sp: span, p: ~[ast::ident], m: ast::ident)
let pathexpr = mk_path(cx, sp, p);
return mk_access_(cx, sp, pathexpr, m);
}
fn mk_addr_of(cx: ext_ctxt, sp: span, e: @ast::expr) -> @ast::expr {
return mk_expr(cx, sp, ast::expr_addr_of(ast::m_imm, e));
}
fn mk_call_(cx: ext_ctxt, sp: span, fn_expr: @ast::expr,
args: ~[@ast::expr]) -> @ast::expr {
mk_expr(cx, sp, ast::expr_call(fn_expr, args, false))

View File

@ -20,10 +20,10 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
let fmtspan = args[0].span;
debug!("Format string:");
log(debug, fmt);
fn parse_fmt_err_(cx: ext_ctxt, sp: span, msg: ~str) -> ! {
fn parse_fmt_err_(cx: ext_ctxt, sp: span, msg: &str) -> ! {
cx.span_fatal(sp, msg);
}
let parse_fmt_err = fn@(s: ~str) -> ! {
let parse_fmt_err = fn@(s: &str) -> ! {
parse_fmt_err_(cx, fmtspan, s)
};
let pieces = parse_fmt_string(fmt, parse_fmt_err);
@ -39,7 +39,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span,
-> @ast::expr {
fn make_path_vec(_cx: ext_ctxt, ident: @~str) -> ~[ast::ident] {
let intr = _cx.parse_sess().interner;
return ~[intr.intern(@~"extfmt"), intr.intern(@~"rt"),
return ~[intr.intern(@~"extfmt"), intr.intern(@~"rt2"),
intr.intern(ident)];
}
fn make_rt_path_expr(cx: ext_ctxt, sp: span, nm: @~str) -> @ast::expr {
@ -187,7 +187,8 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span,
TyFloat => {
return make_conv_call(cx, arg.span, ~"float", cnv, arg);
}
TyPoly => return make_conv_call(cx, arg.span, ~"poly", cnv, arg)
TyPoly => return make_conv_call(cx, arg.span, ~"poly", cnv,
mk_addr_of(cx, sp, arg))
}
}
fn log_conv(c: Conv) {

View File

@ -236,7 +236,7 @@ fn monitor(+f: fn~(diagnostic::emitter)) {
// The 'diagnostics emitter'. Every error, warning, etc. should
// go through this function.
let demitter = fn@(cmsp: Option<(codemap::codemap, codemap::span)>,
msg: ~str, lvl: diagnostic::level) {
msg: &str, lvl: diagnostic::level) {
if lvl == diagnostic::fatal {
comm::send(ch, fatal);
}

View File

@ -145,25 +145,25 @@ fn build_error_handlers(
};
impl DiagnosticHandler: diagnostic::handler {
fn fatal(msg: ~str) -> ! { self.inner.fatal(msg) }
fn err(msg: ~str) { self.inner.err(msg) }
fn fatal(msg: &str) -> ! { self.inner.fatal(msg) }
fn err(msg: &str) { self.inner.err(msg) }
fn bump_err_count() {
self.inner.bump_err_count();
}
fn has_errors() -> bool { self.inner.has_errors() }
fn abort_if_errors() { self.inner.abort_if_errors() }
fn warn(msg: ~str) { self.inner.warn(msg) }
fn note(msg: ~str) { self.inner.note(msg) }
fn bug(msg: ~str) -> ! { self.inner.bug(msg) }
fn unimpl(msg: ~str) -> ! { self.inner.unimpl(msg) }
fn warn(msg: &str) { self.inner.warn(msg) }
fn note(msg: &str) { self.inner.note(msg) }
fn bug(msg: &str) -> ! { self.inner.bug(msg) }
fn unimpl(msg: &str) -> ! { self.inner.unimpl(msg) }
fn emit(cmsp: Option<(codemap::codemap, codemap::span)>,
msg: ~str, lvl: diagnostic::level) {
msg: &str, lvl: diagnostic::level) {
self.inner.emit(cmsp, msg, lvl)
}
}
let emitter = fn@(cmsp: Option<(codemap::codemap, codemap::span)>,
msg: ~str, lvl: diagnostic::level) {
msg: &str, lvl: diagnostic::level) {
diagnostic::emit(cmsp, msg, lvl);
};
let inner_handler = diagnostic::mk_handler(Some(emitter));