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;
|
2012-02-02 18:50:17 -06:00
|
|
|
import syntax::print::pprust::{path_to_str, constr_args_to_str, proto_to_str,
|
|
|
|
mode_to_str};
|
2011-12-07 14:06:12 -06:00
|
|
|
import syntax::{ast, ast_util};
|
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 ty_to_str(cx: ctxt, typ: t) -> str {
|
2012-01-30 23:00:57 -06:00
|
|
|
fn fn_input_to_str(cx: ctxt, input: {mode: ast::mode, ty: t}) ->
|
2011-09-02 17:34:58 -05:00
|
|
|
str {
|
2012-02-03 19:10:29 -06:00
|
|
|
let {mode, ty} = input;
|
|
|
|
let modestr = alt canon_mode(cx, mode) {
|
|
|
|
ast::infer(_) { "" }
|
|
|
|
ast::expl(m) {
|
2012-02-03 08:15:28 -06:00
|
|
|
if !ty::type_has_vars(ty) &&
|
|
|
|
m == ty::default_arg_mode_for_ty(ty) {
|
2012-02-03 19:10:29 -06:00
|
|
|
""
|
|
|
|
} else {
|
|
|
|
mode_to_str(ast::expl(m))
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 04:32:38 -06:00
|
|
|
};
|
2012-02-03 19:10:29 -06:00
|
|
|
modestr + ty_to_str(cx, ty)
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2012-01-31 19:05:20 -06:00
|
|
|
fn fn_to_str(cx: ctxt, proto: ast::proto, ident: option<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 += ")";
|
2012-02-03 08:15:28 -06:00
|
|
|
if ty::get(output).struct != ty_nil {
|
2011-09-14 04:01:42 -05:00
|
|
|
s += " -> ";
|
2011-09-14 10:18:48 -05:00
|
|
|
alt cf {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::noreturn { s += "!"; }
|
2012-01-19 03:03:57 -06:00
|
|
|
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-12-23 09:09:52 -06:00
|
|
|
ret fn_to_str(cx, m.fty.proto, some(m.ident), m.fty.inputs,
|
|
|
|
m.fty.output, m.fty.ret_style, m.fty.constraints) + ";";
|
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 {
|
2012-01-19 00:37:22 -06:00
|
|
|
ast::mut { mstr = "mutable "; }
|
|
|
|
ast::imm { mstr = ""; }
|
|
|
|
ast::maybe_mut { mstr = "const "; }
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
ret mstr + ty_to_str(cx, m.ty);
|
|
|
|
}
|
2012-02-03 08:15:28 -06:00
|
|
|
alt ty::type_name(typ) {
|
2011-12-19 06:52:58 -06:00
|
|
|
some(cs) {
|
2012-02-03 08:15:28 -06:00
|
|
|
alt ty::get(typ).struct {
|
2012-01-25 07:34:31 -06:00
|
|
|
ty_enum(_, tps) | ty_res(_, _, tps) {
|
2011-12-19 06:52:58 -06:00
|
|
|
if vec::len(tps) > 0u {
|
|
|
|
let strs = vec::map(tps, {|t| ty_to_str(cx, t)});
|
2012-02-03 08:15:28 -06:00
|
|
|
ret cs + "<" + str::connect(strs, ",") + ">";
|
2011-12-19 06:52:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
2012-02-03 08:15:28 -06:00
|
|
|
ret cs;
|
2011-12-19 06:52:58 -06:00
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
2012-02-03 08:15:28 -06:00
|
|
|
ret alt ty::get(typ).struct {
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_nil { "()" }
|
|
|
|
ty_bot { "_|_" }
|
|
|
|
ty_bool { "bool" }
|
|
|
|
ty_int(ast::ty_i) { "int" }
|
|
|
|
ty_int(ast::ty_char) { "char" }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_int(t) { ast_util::int_ty_to_str(t) }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_uint(ast::ty_u) { "uint" }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_uint(t) { ast_util::uint_ty_to_str(t) }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_float(ast::ty_f) { "float" }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_float(t) { ast_util::float_ty_to_str(t) }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_str { "str" }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_box(tm) { "@" + mt_to_str(cx, tm) }
|
|
|
|
ty_uniq(tm) { "~" + mt_to_str(cx, tm) }
|
2012-01-02 08:42:13 -06:00
|
|
|
ty_ptr(tm) { "*" + mt_to_str(cx, tm) }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_vec(tm) { "[" + mt_to_str(cx, tm) + "]" }
|
2012-01-19 00:37:22 -06:00
|
|
|
ty_type { "type" }
|
2011-12-07 14:06:12 -06:00
|
|
|
ty_rec(elems) {
|
|
|
|
let strs: [str] = [];
|
|
|
|
for fld: field in elems { strs += [field_to_str(cx, fld)]; }
|
|
|
|
"{" + str::connect(strs, ",") + "}"
|
|
|
|
}
|
|
|
|
ty_tup(elems) {
|
|
|
|
let strs = [];
|
|
|
|
for elem in elems { strs += [ty_to_str(cx, elem)]; }
|
|
|
|
"(" + str::connect(strs, ",") + ")"
|
|
|
|
}
|
2011-12-23 09:09:52 -06:00
|
|
|
ty_fn(f) {
|
|
|
|
fn_to_str(cx, f.proto, none, f.inputs, f.output, f.ret_style,
|
|
|
|
f.constraints)
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
|
|
|
ty_var(v) { "<T" + int::str(v) + ">" }
|
|
|
|
ty_param(id, _) {
|
2012-01-25 03:07:05 -06:00
|
|
|
"'" + str::from_bytes([('a' as u8) + (id as u8)])
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2012-02-10 08:01:32 -06:00
|
|
|
ty_enum(did, tps) {
|
|
|
|
let path = ty::item_path(cx, did);
|
|
|
|
let base = ast_map::path_to_str(path);
|
|
|
|
if vec::is_empty(tps) {
|
|
|
|
base
|
|
|
|
} else {
|
|
|
|
let tps_strs = vec::map(tps) {|t| ty_to_str(cx, t) };
|
|
|
|
#fmt["%s<%s>", base, str::connect(tps_strs, ",")]
|
|
|
|
}
|
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
_ { ty_to_short_str(cx, typ) }
|
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2012-02-07 01:06:21 -06:00
|
|
|
fn ty_to_short_str(cx: ctxt, typ: t) -> str unsafe {
|
2011-07-27 07:19:39 -05:00
|
|
|
let s = encoder::encoded_ty(cx, typ);
|
2012-02-07 01:06:21 -06:00
|
|
|
if str::byte_len(s) >= 32u { s = str::unsafe::slice_bytes(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:
|