2011-09-12 18:13:28 -05:00
|
|
|
import std::{vec, str, int, option};
|
|
|
|
import std::option::{none, some};
|
2011-07-05 04:48:19 -05:00
|
|
|
import middle::ty;
|
|
|
|
import middle::ty::*;
|
2011-07-07 14:47:39 -05:00
|
|
|
import metadata::encoder;
|
2011-11-10 10:41:42 -06:00
|
|
|
import syntax::print::pprust;
|
2011-09-12 18:13:28 -05:00
|
|
|
import syntax::print::pprust::{path_to_str, constr_args_to_str, proto_to_str};
|
2011-08-21 23:44:41 -05:00
|
|
|
import syntax::ast_util::ty_mach_to_str;
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::ast;
|
2011-08-18 19:59:17 -05:00
|
|
|
import middle::ast_map;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn mode_str(m: ty::mode) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt m {
|
2011-10-06 05:26:12 -05:00
|
|
|
ast::by_ref. { "&&" }
|
2011-11-16 05:32:38 -06:00
|
|
|
ast::by_val. { "++" }
|
2011-09-12 03:05:40 -05:00
|
|
|
ast::by_mut_ref. { "&" }
|
|
|
|
ast::by_move. { "-" }
|
2011-11-16 05:32:38 -06:00
|
|
|
ast::by_copy. { "+" }
|
2011-10-06 05:26:12 -05:00
|
|
|
_ { "" }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn mode_str_1(m: ty::mode) -> str {
|
2011-09-12 03:05:40 -05:00
|
|
|
alt m { ast::by_ref. { "ref" } _ { mode_str(m) } }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn fn_ident_to_string(id: ast::node_id, i: ast::fn_ident) -> str {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret alt i { none. { "anon" + int::str(id) } some(s) { s } };
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_id_ident(cx: ctxt, id: ast::def_id) -> str {
|
2011-08-19 17:16:48 -05:00
|
|
|
if id.crate != ast::local_crate {
|
2011-08-30 18:19:52 -05:00
|
|
|
alt cx.ext_map.find(id) {
|
2011-09-02 17:34:58 -05:00
|
|
|
some(j) { str::connect(j, "::") }
|
2011-09-13 10:45:16 -05:00
|
|
|
none. { "<#" + int::str(id.crate) + ":" + int::str(id.node) + ">" }
|
2011-08-30 18:19:52 -05:00
|
|
|
}
|
2011-08-18 19:59:17 -05:00
|
|
|
} else {
|
|
|
|
alt cx.items.find(id.node) {
|
2011-08-27 01:39:08 -05:00
|
|
|
some(ast_map::node_item(it)) { it.ident }
|
2011-08-18 19:59:17 -05:00
|
|
|
_ { fail "get_id_ident: can't find item in ast_map" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -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-09-02 17:34:58 -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-09-12 04:27:30 -05:00
|
|
|
fn fn_to_str(cx: ctxt, proto: ast::proto, ident: option::t<ast::ident>,
|
2011-09-14 03:38:23 -05:00
|
|
|
inputs: [arg], output: t, cf: ast::ret_style,
|
2011-09-12 04:27:30 -05:00
|
|
|
constrs: [@constr]) -> str {
|
2011-08-27 03:16:40 -05:00
|
|
|
let s = proto_to_str(proto);
|
2011-09-02 17:34:58 -05:00
|
|
|
alt ident { some(i) { s += " "; s += i; } _ { } }
|
|
|
|
s += "(";
|
2011-08-19 17:16:48 -05:00
|
|
|
let strs = [];
|
|
|
|
for a: arg in inputs { strs += [fn_input_to_str(cx, a)]; }
|
2011-09-02 17:34:58 -05:00
|
|
|
s += str::connect(strs, ", ");
|
|
|
|
s += ")";
|
2011-07-27 07:19:39 -05:00
|
|
|
if struct(cx, output) != ty_nil {
|
2011-09-14 04:01:42 -05:00
|
|
|
s += " -> ";
|
2011-09-14 10:18:48 -05:00
|
|
|
alt cf {
|
|
|
|
ast::noreturn. { s += "!"; }
|
|
|
|
ast::return_val. { s += ty_to_str(cx, output); }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s += constrs_str(constrs);
|
|
|
|
ret s;
|
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn method_to_str(cx: ctxt, m: method) -> str {
|
2011-08-13 02:09:25 -05:00
|
|
|
ret fn_to_str(cx, m.proto, some::<ast::ident>(m.ident), m.inputs,
|
2011-09-02 17:34:58 -05:00
|
|
|
m.output, m.cf, m.constrs) + ";";
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn field_to_str(cx: ctxt, f: field) -> str {
|
2011-09-02 17:34:58 -05:00
|
|
|
ret f.ident + ": " + mt_to_str(cx, f.mt);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn mt_to_str(cx: ctxt, m: mt) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let mstr;
|
|
|
|
alt m.mut {
|
2011-09-02 17:34:58 -05:00
|
|
|
ast::mut. { mstr = "mutable "; }
|
|
|
|
ast::imm. { mstr = ""; }
|
2011-11-16 16:41:32 -06:00
|
|
|
ast::maybe_mut. { mstr = "const "; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
ret mstr + ty_to_str(cx, m.ty);
|
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
alt cname(cx, typ) { some(cs) { ret cs; } _ { } }
|
2011-08-18 20:03:32 -05:00
|
|
|
ret alt struct(cx, typ) {
|
2011-09-02 17:34:58 -05:00
|
|
|
ty_native(_) { "native" }
|
|
|
|
ty_nil. { "()" }
|
|
|
|
ty_bot. { "_|_" }
|
|
|
|
ty_bool. { "bool" }
|
|
|
|
ty_int. { "int" }
|
|
|
|
ty_float. { "float" }
|
|
|
|
ty_uint. { "uint" }
|
2011-08-27 16:42:29 -05:00
|
|
|
ty_machine(tm) { ty_mach_to_str(tm) }
|
2011-09-02 17:34:58 -05:00
|
|
|
ty_char. { "char" }
|
2011-09-02 18:45:00 -05:00
|
|
|
ty_str. { "str" }
|
2011-09-02 17:34:58 -05:00
|
|
|
ty_box(tm) { "@" + mt_to_str(cx, tm) }
|
2011-09-21 20:54:54 -05:00
|
|
|
ty_uniq(tm) { "~" + mt_to_str(cx, tm) }
|
2011-09-02 17:34:58 -05:00
|
|
|
ty_vec(tm) { "[" + mt_to_str(cx, tm) + "]" }
|
|
|
|
ty_type. { "type" }
|
2011-08-19 17:16:48 -05:00
|
|
|
ty_rec(elems) {
|
2011-09-02 17:34:58 -05:00
|
|
|
let strs: [str] = [];
|
2011-08-19 17:16:48 -05:00
|
|
|
for fld: field in elems { strs += [field_to_str(cx, fld)]; }
|
2011-09-02 17:34:58 -05:00
|
|
|
"{" + str::connect(strs, ",") + "}"
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
|
|
|
ty_tup(elems) {
|
|
|
|
let strs = [];
|
|
|
|
for elem in elems { strs += [ty_to_str(cx, elem)]; }
|
2011-09-02 17:34:58 -05:00
|
|
|
"(" + str::connect(strs, ",") + ")"
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
|
|
|
ty_tag(id, tps) {
|
|
|
|
let s = get_id_ident(cx, id);
|
|
|
|
if vec::len::<t>(tps) > 0u {
|
2011-09-02 17:34:58 -05:00
|
|
|
let strs: [str] = [];
|
2011-08-19 17:16:48 -05:00
|
|
|
for typ: t in tps { strs += [ty_to_str(cx, typ)]; }
|
2011-11-02 05:27:32 -05:00
|
|
|
s += "<" + str::connect(strs, ",") + ">";
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
ty_fn(proto, inputs, output, cf, constrs) {
|
|
|
|
fn_to_str(cx, proto, none, inputs, output, cf, constrs)
|
|
|
|
}
|
2011-11-20 12:15:40 -06:00
|
|
|
ty_native_fn(inputs, output) {
|
2011-10-18 23:18:55 -05:00
|
|
|
fn_to_str(cx, ast::proto_bare, none, inputs, output,
|
2011-09-14 03:38:23 -05:00
|
|
|
ast::return_val, [])
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
|
|
|
ty_obj(meths) {
|
|
|
|
let strs = [];
|
|
|
|
for m: method in meths { strs += [method_to_str(cx, m)]; }
|
2011-09-02 17:34:58 -05:00
|
|
|
"obj {\n\t" + str::connect(strs, "\n\t") + "\n}"
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
|
|
|
ty_res(id, _, _) { get_id_ident(cx, id) }
|
2011-09-02 17:34:58 -05:00
|
|
|
ty_var(v) { "<T" + int::str(v) + ">" }
|
2011-08-19 17:16:48 -05:00
|
|
|
ty_param(id, _) {
|
2011-09-02 17:34:58 -05:00
|
|
|
"'" + str::unsafe_from_bytes([('a' as u8) + (id as u8)])
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
2011-08-27 01:39:08 -05:00
|
|
|
_ { ty_to_short_str(cx, typ) }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn ty_to_short_str(cx: ctxt, typ: t) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = encoder::encoded_ty(cx, typ);
|
2011-09-01 19:27:58 -05:00
|
|
|
if str::byte_len(s) >= 32u { s = str::substr(s, 0u, 32u); }
|
2011-07-05 04:48:19 -05:00
|
|
|
ret s;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn constr_to_str(c: @constr) -> str {
|
2011-08-27 03:16:40 -05:00
|
|
|
ret path_to_str(c.node.path) +
|
2011-09-02 17:34:58 -05:00
|
|
|
pprust::constr_args_to_str(pprust::uint_to_str, c.node.args);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn constrs_str(constrs: [@constr]) -> str {
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = "";
|
2011-07-27 07:19:39 -05:00
|
|
|
let colon = true;
|
2011-08-15 23:54:52 -05:00
|
|
|
for c: @constr in constrs {
|
2011-09-02 17:34:58 -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-11-30 06:38:38 -06:00
|
|
|
fn ty_constr_to_str<Q>(c: @ast::spanned<ast::constr_general_<@ast::path, Q>>)
|
2011-09-02 17:34:58 -05:00
|
|
|
-> str {
|
2011-08-27 03:16:40 -05:00
|
|
|
ret path_to_str(c.node.path) +
|
2011-11-30 06:38:38 -06: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
|
|
|
|
// End:
|