2011-08-15 18:38:23 -05:00
|
|
|
import std::vec;
|
2011-07-05 04:48:19 -05:00
|
|
|
import std::str;
|
2011-07-06 09:46:17 -05:00
|
|
|
import std::int;
|
2011-07-05 04:48:19 -05:00
|
|
|
import std::option;
|
|
|
|
import std::option::none;
|
|
|
|
import std::option::some;
|
|
|
|
import middle::ty;
|
|
|
|
import middle::ty::*;
|
2011-07-07 14:47:39 -05:00
|
|
|
import metadata::encoder;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::print::pp;
|
|
|
|
import syntax::print::pprust;
|
2011-07-19 19:52:34 -05:00
|
|
|
import syntax::print::pprust::path_to_str;
|
|
|
|
import syntax::print::pprust::constr_args_to_str;
|
2011-07-19 22:11:48 -05:00
|
|
|
import syntax::print::pprust::proto_to_str;
|
2011-07-05 04:48:19 -05:00
|
|
|
import pp::word;
|
|
|
|
import pp::eof;
|
|
|
|
import pp::zerobreak;
|
|
|
|
import pp::hardbreak;
|
|
|
|
import ast::ty_mach_to_str;
|
|
|
|
import syntax::ast;
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn mode_str(m: &ty::mode) -> str {
|
|
|
|
alt m {
|
|
|
|
mo_val. { "" }
|
|
|
|
mo_alias(false) { "&" }
|
|
|
|
mo_alias(true) { "&mutable " }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn mode_str_1(m: &ty::mode) -> str {
|
|
|
|
alt m { mo_val. { "val" } _ { mode_str(m) } }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn fn_ident_to_string(id: ast::node_id, i: &ast::fn_ident) -> str {
|
|
|
|
ret alt i { none. { "anon" + int::str(id) } some(s) { s } };
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn ty_to_str(cx: &ctxt, typ: &t) -> str {
|
|
|
|
fn fn_input_to_str(cx: &ctxt, input: &{mode: middle::ty::mode, ty: t}) ->
|
2011-07-05 04:48:19 -05:00
|
|
|
str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = mode_str(input.mode);
|
2011-07-05 04:48:19 -05:00
|
|
|
ret s + ty_to_str(cx, input.ty);
|
|
|
|
}
|
2011-08-12 09:15:18 -05:00
|
|
|
fn fn_to_str(cx: &ctxt, proto: ast::proto, ident: option::t<ast::ident>,
|
2011-08-04 18:20:09 -05:00
|
|
|
inputs: &[arg], output: t, cf: ast::controlflow,
|
|
|
|
constrs: &[@constr]) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = proto_to_str(proto);
|
|
|
|
alt ident { some(i) { s += " "; s += i; } _ { } }
|
2011-07-05 04:48:19 -05:00
|
|
|
s += "(";
|
2011-08-11 19:54:42 -05:00
|
|
|
let strs = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: arg in inputs { strs += ~[fn_input_to_str(cx, a)]; }
|
2011-08-11 19:56:57 -05:00
|
|
|
s += str::connect(strs, ", ");
|
2011-07-05 04:48:19 -05:00
|
|
|
s += ")";
|
2011-07-27 07:19:39 -05:00
|
|
|
if struct(cx, output) != ty_nil {
|
|
|
|
alt cf {
|
|
|
|
ast::noreturn. { s += " -> !"; }
|
|
|
|
ast::return. { s += " -> " + ty_to_str(cx, output); }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s += constrs_str(constrs);
|
|
|
|
ret s;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn method_to_str(cx: &ctxt, m: &method) -> str {
|
2011-07-05 04:48:19 -05:00
|
|
|
ret fn_to_str(cx, m.proto, some[ast::ident](m.ident), m.inputs,
|
|
|
|
m.output, m.cf, m.constrs) + ";";
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn field_to_str(cx: &ctxt, f: &field) -> str {
|
2011-08-02 04:42:29 -05:00
|
|
|
ret f.ident + ": " + mt_to_str(cx, f.mt);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn mt_to_str(cx: &ctxt, m: &mt) -> str {
|
|
|
|
let mstr;
|
|
|
|
alt m.mut {
|
|
|
|
ast::mut. { mstr = "mutable "; }
|
|
|
|
ast::imm. { mstr = ""; }
|
|
|
|
ast::maybe_mut. { mstr = "mutable? "; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
ret mstr + ty_to_str(cx, m.ty);
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
alt cname(cx, typ) { some(cs) { ret cs; } _ { } }
|
|
|
|
let s = "";
|
|
|
|
alt struct(cx, typ) {
|
|
|
|
ty_native(_) { s += "native"; }
|
|
|
|
ty_nil. { s += "()"; }
|
|
|
|
ty_bot. { s += "_|_"; }
|
|
|
|
ty_bool. { s += "bool"; }
|
|
|
|
ty_int. { s += "int"; }
|
|
|
|
ty_float. { s += "float"; }
|
|
|
|
ty_uint. { s += "uint"; }
|
|
|
|
ty_machine(tm) { s += ty_mach_to_str(tm); }
|
|
|
|
ty_char. { s += "char"; }
|
|
|
|
ty_str. { s += "str"; }
|
|
|
|
ty_istr. { s += "istr"; }
|
|
|
|
ty_box(tm) { s += "@" + mt_to_str(cx, tm); }
|
2011-08-10 19:23:46 -05:00
|
|
|
ty_uniq(t) { s += "~" + ty_to_str(cx, t); }
|
2011-08-12 09:15:18 -05:00
|
|
|
ty_vec(tm) { s += "vec<" + mt_to_str(cx, tm) + ">"; }
|
2011-08-10 19:23:46 -05:00
|
|
|
ty_ivec(tm) { s += "[" + mt_to_str(cx, tm) + "]"; }
|
2011-07-27 07:19:39 -05:00
|
|
|
ty_type. { s += "type"; }
|
|
|
|
ty_rec(elems) {
|
2011-08-04 18:20:09 -05:00
|
|
|
let strs: [str] = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for fld: field in elems { strs += ~[field_to_str(cx, fld)]; }
|
2011-08-11 19:56:57 -05:00
|
|
|
s += "{" + str::connect(strs, ",") + "}";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-08-15 04:40:26 -05:00
|
|
|
ty_tup(elems) {
|
|
|
|
let strs = ~[];
|
2011-08-15 05:08:05 -05:00
|
|
|
for elem in elems { strs += ~[ty_to_str(cx, elem)]; }
|
2011-08-15 04:40:26 -05:00
|
|
|
s += "(" + str::connect(strs, ",") + ")";
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
ty_tag(id, tps) {
|
|
|
|
// The user should never see this if the cname is set properly!
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
s += "<tag#" + int::str(id.crate) + ":" + int::str(id.node) + ">";
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len[t](tps) > 0u {
|
2011-08-04 18:20:09 -05:00
|
|
|
let strs: [str] = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for typ: t in tps { strs += ~[ty_to_str(cx, typ)]; }
|
2011-08-11 19:56:57 -05:00
|
|
|
s += "[" + str::connect(strs, ",") + "]";
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
ty_fn(proto, inputs, output, cf, constrs) {
|
|
|
|
s += fn_to_str(cx, proto, none, inputs, output, cf, constrs);
|
|
|
|
}
|
|
|
|
ty_native_fn(_, inputs, output) {
|
|
|
|
s +=
|
|
|
|
fn_to_str(cx, ast::proto_fn, none, inputs, output, ast::return,
|
|
|
|
~[]);
|
|
|
|
}
|
|
|
|
ty_obj(meths) {
|
|
|
|
let strs = ~[];
|
2011-08-15 23:54:52 -05:00
|
|
|
for m: method in meths { strs += ~[method_to_str(cx, m)]; }
|
2011-08-11 19:56:57 -05:00
|
|
|
s += "obj {\n\t" + str::connect(strs, "\n\t") + "\n}";
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
ty_res(id, _, _) {
|
|
|
|
s +=
|
|
|
|
"<resource#" + int::str(id.node) + ":" + int::str(id.crate) + ">";
|
|
|
|
}
|
|
|
|
ty_var(v) { s += "<T" + int::str(v) + ">"; }
|
2011-07-28 15:29:29 -05:00
|
|
|
ty_param(id,_) {
|
2011-08-11 19:13:53 -05:00
|
|
|
s += "'" + str::unsafe_from_bytes(~[('a' as u8) + (id as u8)]);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
|
|
|
_ { s += ty_to_short_str(cx, typ); }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn ty_to_short_str(cx: &ctxt, typ: t) -> str {
|
|
|
|
let s = encoder::encoded_ty(cx, typ);
|
|
|
|
if str::byte_len(s) >= 32u { s = str::substr(s, 0u, 32u); }
|
2011-07-05 04:48:19 -05:00
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn constr_to_str(c: &@constr) -> str {
|
2011-07-19 19:52:34 -05:00
|
|
|
ret path_to_str(c.node.path) +
|
2011-07-27 07:19:39 -05:00
|
|
|
pprust::constr_args_to_str(pprust::uint_to_str, c.node.args);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn constrs_str(constrs: &[@constr]) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = "";
|
|
|
|
let colon = true;
|
2011-08-15 23:54:52 -05:00
|
|
|
for c: @constr in constrs {
|
2011-07-27 07:19:39 -05:00
|
|
|
if colon { s += " : "; colon = false; } else { s += ", "; }
|
2011-07-05 04:48:19 -05:00
|
|
|
s += constr_to_str(c);
|
|
|
|
}
|
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
2011-08-12 08:36:51 -05:00
|
|
|
fn ty_constr_to_str<Q>(c: &@ast::spanned<ast::constr_general_<ast::path, Q>>)
|
2011-07-27 07:19:39 -05:00
|
|
|
-> str {
|
2011-07-19 19:52:34 -05:00
|
|
|
ret path_to_str(c.node.path) +
|
2011-07-27 07:19:39 -05:00
|
|
|
constr_args_to_str[ast::path](path_to_str, c.node.args);
|
2011-07-19 19:52:34 -05:00
|
|
|
}
|
|
|
|
|
2011-07-05 04:48:19 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|