2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2012-09-04 13:54:36 -05:00
|
|
|
use middle::ty;
|
2013-03-26 15:38:07 -05:00
|
|
|
use middle::ty::canon_mode;
|
2012-12-05 17:13:24 -06:00
|
|
|
use middle::ty::{bound_region, br_anon, br_named, br_self, br_cap_avoid,
|
|
|
|
br_fresh};
|
2012-11-04 22:41:00 -06:00
|
|
|
use middle::ty::{ctxt, field, method};
|
2013-01-25 18:57:39 -06:00
|
|
|
use middle::ty::{mt, t, param_bound, param_ty};
|
2012-10-19 08:01:01 -05:00
|
|
|
use middle::ty::{re_bound, re_free, re_scope, re_infer, re_static, Region};
|
|
|
|
use middle::ty::{ReSkolemized, ReVar};
|
2012-12-10 15:47:54 -06:00
|
|
|
use middle::ty::{ty_bool, ty_bot, ty_box, ty_struct, ty_enum};
|
2013-01-31 19:12:29 -06:00
|
|
|
use middle::ty::{ty_err, ty_estr, ty_evec, ty_float, ty_bare_fn, ty_closure};
|
|
|
|
use middle::ty::{ty_trait, ty_int};
|
2012-09-04 13:54:36 -05:00
|
|
|
use middle::ty::{ty_nil, ty_opaque_box, ty_opaque_closure_ptr, ty_param};
|
2013-03-05 20:06:53 -06:00
|
|
|
use middle::ty::{ty_ptr, ty_rptr, ty_self, ty_tup, ty_type, ty_uniq};
|
|
|
|
use middle::ty::{ty_uint, ty_unboxed_vec, ty_infer};
|
2012-09-04 13:54:36 -05:00
|
|
|
use metadata::encoder;
|
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::print::pprust;
|
2013-03-26 15:38:07 -05:00
|
|
|
use syntax::print::pprust::mode_to_str;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, ast_util};
|
|
|
|
use syntax::ast_map;
|
2013-03-13 21:25:28 -05:00
|
|
|
use syntax::abi::AbiSet;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn note_and_explain_region(cx: ctxt,
|
|
|
|
prefix: ~str,
|
|
|
|
region: ty::Region,
|
|
|
|
suffix: ~str) {
|
2012-08-13 17:06:13 -05:00
|
|
|
match explain_region_and_span(cx, region) {
|
2012-12-04 12:50:00 -06:00
|
|
|
(ref str, Some(span)) => {
|
2012-08-13 17:06:13 -05:00
|
|
|
cx.sess.span_note(
|
|
|
|
span,
|
2012-12-04 12:50:00 -06:00
|
|
|
fmt!("%s%s%s", prefix, (*str), suffix));
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
(ref str, None) => {
|
2012-08-13 17:06:13 -05:00
|
|
|
cx.sess.note(
|
2012-12-04 12:50:00 -06:00
|
|
|
fmt!("%s%s%s", prefix, (*str), suffix));
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a string like "the block at 27:31" that attempts to explain a
|
|
|
|
/// lifetime in a way it might plausibly be understood.
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn explain_region(cx: ctxt, region: ty::Region) -> ~str {
|
2012-08-13 17:06:13 -05:00
|
|
|
let (res, _) = explain_region_and_span(cx, region);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn explain_region_and_span(cx: ctxt, region: ty::Region)
|
|
|
|
-> (~str, Option<span>) {
|
2012-08-06 14:34:08 -05:00
|
|
|
return match region {
|
2012-07-31 23:08:24 -05:00
|
|
|
re_scope(node_id) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match cx.items.find(&node_id) {
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_block(ref blk)) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
explain_span(cx, "block", blk.span)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_expr(expr)) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2013-01-05 21:33:37 -06:00
|
|
|
ast::expr_call(*) => explain_span(cx, "call", expr.span),
|
2012-11-30 13:18:25 -06:00
|
|
|
ast::expr_method_call(*) => {
|
2013-01-05 21:33:37 -06:00
|
|
|
explain_span(cx, "method call", expr.span)
|
2012-11-30 13:18:25 -06:00
|
|
|
},
|
2013-01-05 21:33:37 -06:00
|
|
|
ast::expr_match(*) => explain_span(cx, "match", expr.span),
|
|
|
|
_ => explain_span(cx, "expression", expr.span)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_stmt(stmt)) => {
|
2013-01-19 19:06:36 -06:00
|
|
|
explain_span(cx, "statement", stmt.span)
|
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_item(it, _)) if (match it.node {
|
2013-01-20 21:47:04 -06:00
|
|
|
ast::item_fn(*) => true, _ => false}) => {
|
|
|
|
explain_span(cx, "function body", it.span)
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) | None => {
|
2012-07-31 23:08:24 -05:00
|
|
|
// this really should not happen
|
2012-08-13 17:06:13 -05:00
|
|
|
(fmt!("unknown scope: %d. Please report a bug.", node_id),
|
2012-08-20 14:23:37 -05:00
|
|
|
None)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
2012-08-07 21:48:24 -05:00
|
|
|
}
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
re_free(id, br) => {
|
2012-08-20 18:53:33 -05:00
|
|
|
let prefix = match br {
|
|
|
|
br_anon(idx) => fmt!("the anonymous lifetime #%u defined on",
|
|
|
|
idx + 1),
|
2012-12-05 17:13:24 -06:00
|
|
|
br_fresh(_) => fmt!("an anonymous lifetime defined on"),
|
2012-08-20 18:53:33 -05:00
|
|
|
_ => fmt!("the lifetime %s as defined on",
|
|
|
|
bound_region_to_str(cx, br))
|
|
|
|
};
|
|
|
|
|
2013-02-05 21:41:45 -06:00
|
|
|
match cx.items.find(&id) {
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_block(ref blk)) => {
|
2013-02-18 00:20:36 -06:00
|
|
|
let (msg, opt_span) = explain_span(cx, "block", blk.span);
|
2012-08-20 18:53:33 -05:00
|
|
|
(fmt!("%s %s", prefix, msg), opt_span)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) | None => {
|
2012-07-31 23:08:24 -05:00
|
|
|
// this really should not happen
|
2012-08-20 14:23:37 -05:00
|
|
|
(fmt!("%s node %d", prefix, id), None)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
re_static => { (~"the static lifetime", None) }
|
2012-07-31 23:08:24 -05:00
|
|
|
|
2012-08-07 21:48:24 -05:00
|
|
|
// I believe these cases should not occur (except when debugging,
|
|
|
|
// perhaps)
|
2012-10-19 08:01:01 -05:00
|
|
|
re_infer(_) | re_bound(_) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
(fmt!("lifetime %?", region), None)
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-05 21:33:37 -06:00
|
|
|
fn explain_span(cx: ctxt, heading: &str, span: span)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> (~str, Option<span>)
|
2012-08-13 17:06:13 -05:00
|
|
|
{
|
2012-11-12 20:24:56 -06:00
|
|
|
let lo = cx.sess.codemap.lookup_char_pos_adj(span.lo);
|
2012-11-12 21:32:48 -06:00
|
|
|
(fmt!("the %s at %u:%u", heading,
|
|
|
|
lo.line, lo.col.to_uint()), Some(span))
|
2012-07-31 23:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn bound_region_to_str(cx: ctxt, br: bound_region) -> ~str {
|
2013-03-14 14:25:48 -05:00
|
|
|
bound_region_to_str_space(cx, "&", br)
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
2012-08-20 18:53:33 -05:00
|
|
|
|
2013-03-14 14:25:48 -05:00
|
|
|
pub fn bound_region_to_str_space(cx: ctxt,
|
|
|
|
prefix: &str,
|
|
|
|
br: bound_region)
|
|
|
|
-> ~str {
|
|
|
|
if cx.sess.verbose() { return fmt!("%s%? ", prefix, br); }
|
2012-07-24 18:23:23 -05:00
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
match br {
|
2013-03-14 14:25:48 -05:00
|
|
|
br_named(id) => fmt!("%s'%s ", prefix, *cx.sess.str_of(id)),
|
|
|
|
br_self => fmt!("%s'self ", prefix),
|
2013-01-05 21:33:37 -06:00
|
|
|
br_anon(_) => prefix.to_str(),
|
2012-12-05 17:13:24 -06:00
|
|
|
br_fresh(_) => prefix.to_str(),
|
2013-03-14 14:25:48 -05:00
|
|
|
br_cap_avoid(_, br) => bound_region_to_str_space(cx, prefix, *br)
|
2012-04-01 16:28:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn re_scope_id_to_str(cx: ctxt, node_id: ast::node_id) -> ~str {
|
2013-02-05 21:41:45 -06:00
|
|
|
match cx.items.find(&node_id) {
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_block(ref blk)) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("<block at %s>",
|
2013-02-18 00:20:36 -06:00
|
|
|
cx.sess.codemap.span_to_str(blk.span))
|
2012-04-12 23:59:33 -05:00
|
|
|
}
|
2013-03-23 20:45:27 -05:00
|
|
|
Some(&ast_map::node_expr(expr)) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_call(*) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("<call at %s>",
|
2012-11-12 20:24:56 -06:00
|
|
|
cx.sess.codemap.span_to_str(expr.span))
|
2012-04-12 23:59:33 -05:00
|
|
|
}
|
2012-08-07 15:35:51 -05:00
|
|
|
ast::expr_match(*) => {
|
2013-01-04 08:52:07 -06:00
|
|
|
fmt!("<match at %s>",
|
2012-11-12 20:24:56 -06:00
|
|
|
cx.sess.codemap.span_to_str(expr.span))
|
2012-04-12 23:59:33 -05:00
|
|
|
}
|
2012-07-06 11:14:57 -05:00
|
|
|
ast::expr_assign_op(*) |
|
|
|
|
ast::expr_field(*) |
|
|
|
|
ast::expr_unary(*) |
|
|
|
|
ast::expr_binary(*) |
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_index(*) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("<method at %s>",
|
2012-11-12 20:24:56 -06:00
|
|
|
cx.sess.codemap.span_to_str(expr.span))
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("<expression at %s>",
|
2012-11-12 20:24:56 -06:00
|
|
|
cx.sess.codemap.span_to_str(expr.span))
|
2012-07-26 10:51:57 -05:00
|
|
|
}
|
2012-03-15 21:05:38 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("<unknown-%d>", node_id)
|
2012-07-06 15:59:17 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { cx.sess.bug(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("re_scope refers to %s",
|
2012-07-18 18:18:02 -05:00
|
|
|
ast_map::node_id_to_str(cx.items, node_id,
|
2012-08-22 19:24:52 -05:00
|
|
|
cx.sess.parse_sess.interner))) }
|
2012-04-12 23:59:33 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2012-08-13 17:06:13 -05:00
|
|
|
// In general, if you are giving a region error message,
|
|
|
|
// you should use `explain_region()` or, better yet,
|
|
|
|
// `note_and_explain_region()`
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn region_to_str(cx: ctxt, region: Region) -> ~str {
|
2013-03-14 14:25:48 -05:00
|
|
|
region_to_str_space(cx, "&", region)
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-14 14:25:48 -05:00
|
|
|
pub fn region_to_str_space(cx: ctxt, prefix: &str, region: Region) -> ~str {
|
2012-09-12 19:06:36 -05:00
|
|
|
if cx.sess.verbose() {
|
2013-03-14 14:25:48 -05:00
|
|
|
return fmt!("%s%? ", prefix, region);
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2012-08-13 17:06:13 -05:00
|
|
|
// These printouts are concise. They do not contain all the information
|
|
|
|
// the user might want to diagnose an error, but there is basically no way
|
|
|
|
// to fit that into a short string. Hence the recommendation to use
|
|
|
|
// `explain_region()` or `note_and_explain_region()`.
|
|
|
|
match region {
|
2013-01-05 21:33:37 -06:00
|
|
|
re_scope(_) => prefix.to_str(),
|
2013-03-14 14:25:48 -05:00
|
|
|
re_bound(br) => bound_region_to_str_space(cx, prefix, br),
|
|
|
|
re_free(_, br) => bound_region_to_str_space(cx, prefix, br),
|
2012-11-04 22:41:00 -06:00
|
|
|
re_infer(ReSkolemized(_, br)) => {
|
2013-03-14 14:25:48 -05:00
|
|
|
bound_region_to_str_space(cx, prefix, br)
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
2013-01-05 21:33:37 -06:00
|
|
|
re_infer(ReVar(_)) => prefix.to_str(),
|
2013-03-14 14:25:48 -05:00
|
|
|
re_static => fmt!("%s'static ", prefix)
|
2012-03-15 21:05:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 00:17:42 -05:00
|
|
|
pub fn mt_to_str(cx: ctxt, m: &mt) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
let mstr = match m.mutbl {
|
2013-01-05 21:33:37 -06:00
|
|
|
ast::m_mutbl => "mut ",
|
|
|
|
ast::m_imm => "",
|
|
|
|
ast::m_const => "const "
|
2012-03-22 22:06:01 -05:00
|
|
|
};
|
2013-01-05 21:33:37 -06:00
|
|
|
return fmt!("%s%s", mstr, ty_to_str(cx, m.ty));
|
2012-03-22 22:06:01 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn vstore_to_str(cx: ctxt, vs: ty::vstore) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match vs {
|
2012-08-22 19:24:52 -05:00
|
|
|
ty::vstore_fixed(n) => fmt!("%u", n),
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::vstore_uniq => ~"~",
|
|
|
|
ty::vstore_box => ~"@",
|
2013-03-14 14:25:48 -05:00
|
|
|
ty::vstore_slice(r) => region_to_str_space(cx, "&", r)
|
2012-04-13 15:35:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-08 23:16:09 -06:00
|
|
|
pub fn trait_store_to_str(cx: ctxt, s: ty::TraitStore) -> ~str {
|
|
|
|
match s {
|
|
|
|
ty::BareTraitStore => ~"",
|
|
|
|
ty::UniqTraitStore => ~"~",
|
|
|
|
ty::BoxTraitStore => ~"@",
|
2013-03-14 14:25:48 -05:00
|
|
|
ty::RegionTraitStore(r) => region_to_str_space(cx, "&", r)
|
2013-03-08 23:16:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn vstore_ty_to_str(cx: ctxt, ty: ~str, vs: ty::vstore) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match vs {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::vstore_fixed(_) => {
|
2013-02-19 01:55:37 -06:00
|
|
|
fmt!("[%s * %s]", ty, vstore_to_str(cx, vs))
|
2012-07-13 17:24:41 -05:00
|
|
|
}
|
2012-08-24 17:28:43 -05:00
|
|
|
ty::vstore_slice(_) => {
|
2013-03-14 14:25:48 -05:00
|
|
|
fmt!("%s %s", vstore_to_str(cx, vs), ty)
|
2012-08-24 17:28:43 -05:00
|
|
|
}
|
2013-02-19 01:55:37 -06:00
|
|
|
_ => fmt!("%s[%s]", vstore_to_str(cx, vs), ty)
|
2012-07-13 17:24:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn expr_repr(cx: ctxt, expr: @ast::expr) -> ~str {
|
2012-09-11 23:25:01 -05:00
|
|
|
fmt!("expr(%d: %s)",
|
|
|
|
expr.id,
|
|
|
|
pprust::expr_to_str(expr, cx.sess.intr()))
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn pat_repr(cx: ctxt, pat: @ast::pat) -> ~str {
|
2013-01-24 18:24:45 -06:00
|
|
|
fmt!("pat(%d: %s)",
|
|
|
|
pat.id,
|
|
|
|
pprust::pat_to_str(pat, cx.sess.intr()))
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn tys_to_str(cx: ctxt, ts: &[t]) -> ~str {
|
2012-09-21 20:43:30 -05:00
|
|
|
let tstrs = ts.map(|t| ty_to_str(cx, *t));
|
2013-01-08 16:00:45 -06:00
|
|
|
fmt!("(%s)", str::connect(tstrs, ", "))
|
2012-05-03 11:17:58 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn bound_to_str(cx: ctxt, b: param_bound) -> ~str {
|
2012-12-10 16:22:11 -06:00
|
|
|
ty::param_bound_to_str(cx, &b)
|
2012-09-05 17:36:11 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn fn_sig_to_str(cx: ctxt, typ: &ty::FnSig) -> ~str {
|
2013-01-08 16:00:45 -06:00
|
|
|
fmt!("fn%s -> %s",
|
|
|
|
tys_to_str(cx, typ.inputs.map(|a| a.ty)),
|
|
|
|
ty_to_str(cx, typ.output))
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
|
2013-01-25 18:57:39 -06:00
|
|
|
fn fn_input_to_str(cx: ctxt, input: ty::arg) -> ~str {
|
|
|
|
let ty::arg {mode: mode, ty: ty} = input;
|
2012-08-06 14:34:08 -05:00
|
|
|
let modestr = match canon_mode(cx, mode) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::infer(_) => ~"",
|
|
|
|
ast::expl(m) => {
|
2012-05-09 08:09:58 -05:00
|
|
|
if !ty::type_needs_infer(ty) &&
|
2012-09-18 17:52:21 -05:00
|
|
|
m == ty::default_arg_mode_for_ty(cx, ty) {
|
2012-07-14 00:57:48 -05:00
|
|
|
~""
|
2012-02-03 19:10:29 -06:00
|
|
|
} else {
|
2013-01-05 21:33:37 -06:00
|
|
|
mode_to_str(ast::expl(m)) + ~":"
|
2012-02-03 19:10:29 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 04:32:38 -06:00
|
|
|
};
|
2013-01-05 21:33:37 -06:00
|
|
|
fmt!("%s%s", modestr, ty_to_str(cx, ty))
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
fn bare_fn_to_str(cx: ctxt,
|
|
|
|
purity: ast::purity,
|
2013-03-13 21:25:28 -05:00
|
|
|
abis: AbiSet,
|
2013-01-31 19:12:29 -06:00
|
|
|
ident: Option<ast::ident>,
|
|
|
|
sig: &ty::FnSig) -> ~str
|
|
|
|
{
|
|
|
|
let mut s = ~"extern ";
|
2012-11-02 15:33:51 -05:00
|
|
|
|
2013-03-13 21:25:28 -05:00
|
|
|
s.push_str(abis.to_str());
|
|
|
|
s.push_char(' ');
|
2013-01-31 19:12:29 -06:00
|
|
|
|
|
|
|
match purity {
|
|
|
|
ast::impure_fn => {}
|
|
|
|
_ => {
|
|
|
|
s.push_str(purity.to_str());
|
|
|
|
s.push_char(' ');
|
|
|
|
}
|
2012-05-25 01:44:58 -05:00
|
|
|
};
|
2012-08-10 20:15:08 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
s.push_str("fn");
|
2012-11-04 22:41:00 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
match ident {
|
|
|
|
Some(i) => {
|
|
|
|
s.push_char(' ');
|
2013-02-10 18:33:16 -06:00
|
|
|
s.push_str(*cx.sess.str_of(i));
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
|
|
|
|
push_sig_to_str(cx, &mut s, sig);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
fn closure_to_str(cx: ctxt, cty: &ty::ClosureTy) -> ~str
|
|
|
|
{
|
|
|
|
let mut s = cty.sigil.to_str();
|
|
|
|
|
|
|
|
match (cty.sigil, cty.region) {
|
|
|
|
(ast::ManagedSigil, ty::re_static) |
|
|
|
|
(ast::OwnedSigil, ty::re_static) => {}
|
2012-11-04 22:41:00 -06:00
|
|
|
|
|
|
|
(_, region) => {
|
2013-03-14 14:25:48 -05:00
|
|
|
s.push_str(region_to_str_space(cx, "", region));
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
match cty.purity {
|
|
|
|
ast::impure_fn => {}
|
|
|
|
_ => {
|
|
|
|
s.push_str(cty.purity.to_str());
|
|
|
|
s.push_char(' ');
|
|
|
|
}
|
|
|
|
};
|
2012-08-10 20:15:08 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
match cty.onceness {
|
|
|
|
ast::Many => {}
|
|
|
|
ast::Once => {
|
|
|
|
s.push_str(cty.onceness.to_str());
|
|
|
|
s.push_char(' ');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
s.push_str("fn");
|
|
|
|
|
|
|
|
push_sig_to_str(cx, &mut s, &cty.sig);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
fn push_sig_to_str(cx: ctxt, s: &mut ~str, sig: &ty::FnSig) {
|
|
|
|
s.push_char('(');
|
|
|
|
let strs = sig.inputs.map(|a| fn_input_to_str(cx, *a));
|
|
|
|
s.push_str(str::connect(strs, ", "));
|
|
|
|
s.push_char(')');
|
|
|
|
if ty::get(sig.output).sty != ty_nil {
|
|
|
|
s.push_str(" -> ");
|
|
|
|
if ty::type_is_bot(sig.output) {
|
|
|
|
s.push_char('!');
|
2013-01-08 08:21:19 -06:00
|
|
|
} else {
|
2013-01-31 19:12:29 -06:00
|
|
|
s.push_str(ty_to_str(cx, sig.output));
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn method_to_str(cx: ctxt, m: method) -> ~str {
|
2013-01-31 19:12:29 -06:00
|
|
|
bare_fn_to_str(cx,
|
|
|
|
m.fty.purity,
|
2013-03-13 21:25:28 -05:00
|
|
|
m.fty.abis,
|
2013-01-31 19:12:29 -06:00
|
|
|
Some(m.ident),
|
|
|
|
&m.fty.sig) + ~";"
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
fn field_to_str(cx: ctxt, f: field) -> ~str {
|
2013-03-20 00:17:42 -05:00
|
|
|
return *cx.sess.str_of(f.ident) + ~": " + mt_to_str(cx, &f.mt);
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2012-02-10 12:28:35 -06:00
|
|
|
|
|
|
|
// if there is an id, print that instead of the structural type:
|
2013-01-07 16:16:52 -06:00
|
|
|
/*for ty::type_def_id(typ).each |def_id| {
|
2012-05-04 14:33:04 -05:00
|
|
|
// note that this typedef cannot have type parameters
|
2012-09-19 18:55:01 -05:00
|
|
|
return ast_map::path_to_str(ty::item_path(cx, *def_id),
|
|
|
|
cx.sess.intr());
|
2013-01-07 16:16:52 -06:00
|
|
|
}*/
|
2012-02-10 12:28:35 -06:00
|
|
|
|
|
|
|
// pretty print the structural type representation:
|
2013-03-20 00:17:42 -05:00
|
|
|
return match ty::get(typ).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_nil => ~"()",
|
2013-01-08 08:21:19 -06:00
|
|
|
ty_bot => ~"!",
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_bool => ~"bool",
|
|
|
|
ty_int(ast::ty_i) => ~"int",
|
|
|
|
ty_int(ast::ty_char) => ~"char",
|
|
|
|
ty_int(t) => ast_util::int_ty_to_str(t),
|
|
|
|
ty_uint(ast::ty_u) => ~"uint",
|
|
|
|
ty_uint(t) => ast_util::uint_ty_to_str(t),
|
|
|
|
ty_float(ast::ty_f) => ~"float",
|
|
|
|
ty_float(t) => ast_util::float_ty_to_str(t),
|
2013-03-20 00:17:42 -05:00
|
|
|
ty_box(ref tm) => ~"@" + mt_to_str(cx, tm),
|
|
|
|
ty_uniq(ref tm) => ~"~" + mt_to_str(cx, tm),
|
|
|
|
ty_ptr(ref tm) => ~"*" + mt_to_str(cx, tm),
|
|
|
|
ty_rptr(r, ref tm) => {
|
2013-03-14 14:25:48 -05:00
|
|
|
region_to_str_space(cx, ~"&", r) + mt_to_str(cx, tm)
|
2012-04-13 15:35:57 -05:00
|
|
|
}
|
2013-03-20 00:17:42 -05:00
|
|
|
ty_unboxed_vec(ref tm) => { ~"unboxed_vec<" + mt_to_str(cx, tm) + ~">" }
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_type => ~"type",
|
2013-03-20 00:17:42 -05:00
|
|
|
ty_tup(ref elems) => {
|
2012-10-18 11:14:11 -05:00
|
|
|
let strs = elems.map(|elem| ty_to_str(cx, *elem));
|
2012-07-14 00:57:48 -05:00
|
|
|
~"(" + str::connect(strs, ~",") + ~")"
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ty_closure(ref f) => {
|
|
|
|
closure_to_str(cx, f)
|
|
|
|
}
|
|
|
|
ty_bare_fn(ref f) => {
|
2013-03-13 21:25:28 -05:00
|
|
|
bare_fn_to_str(cx, f.purity, f.abis, None, &f.sig)
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2012-09-06 14:30:15 -05:00
|
|
|
ty_infer(infer_ty) => infer_ty.to_str(),
|
2012-11-16 21:22:48 -06:00
|
|
|
ty_err => ~"[type error]",
|
2013-01-10 12:59:58 -06:00
|
|
|
ty_param(param_ty {idx: id, def_id: did}) => {
|
|
|
|
if cx.sess.verbose() {
|
|
|
|
fmt!("'%s:%?",
|
|
|
|
str::from_bytes(~[('a' as u8) + (id as u8)]),
|
|
|
|
did)
|
|
|
|
} else {
|
|
|
|
fmt!("'%s",
|
|
|
|
str::from_bytes(~[('a' as u8) + (id as u8)]))
|
|
|
|
}
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2013-03-19 09:36:02 -05:00
|
|
|
ty_self(*) => ~"self",
|
2012-12-10 15:47:54 -06:00
|
|
|
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
|
2012-04-18 23:26:25 -05:00
|
|
|
let path = ty::item_path(cx, did);
|
2012-07-18 18:18:02 -05:00
|
|
|
let base = ast_map::path_to_str(path, cx.sess.intr());
|
2013-01-08 08:21:19 -06:00
|
|
|
parameterized(cx, base, substs.self_r, substs.tps)
|
2012-04-18 23:26:25 -05:00
|
|
|
}
|
2013-03-08 23:16:09 -06:00
|
|
|
ty_trait(did, ref substs, s) => {
|
2012-02-10 08:01:32 -06:00
|
|
|
let path = ty::item_path(cx, did);
|
2012-07-18 18:18:02 -05:00
|
|
|
let base = ast_map::path_to_str(path, cx.sess.intr());
|
2013-02-19 01:55:37 -06:00
|
|
|
let ty = parameterized(cx, base, substs.self_r, substs.tps);
|
2013-03-08 23:16:09 -06:00
|
|
|
fmt!("%s%s", trait_store_to_str(cx, s), ty)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
2013-03-20 00:17:42 -05:00
|
|
|
ty_evec(ref mt, vs) => {
|
2013-02-19 01:55:37 -06:00
|
|
|
vstore_ty_to_str(cx, fmt!("%s", mt_to_str(cx, mt)), vs)
|
2012-04-13 15:35:57 -05:00
|
|
|
}
|
2013-02-19 01:55:37 -06:00
|
|
|
ty_estr(vs) => fmt!("%s%s", vstore_to_str(cx, vs), ~"str"),
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_opaque_box => ~"@?",
|
2013-01-31 19:12:29 -06:00
|
|
|
ty_opaque_closure_ptr(ast::BorrowedSigil) => ~"closure&",
|
|
|
|
ty_opaque_closure_ptr(ast::ManagedSigil) => ~"closure@",
|
|
|
|
ty_opaque_closure_ptr(ast::OwnedSigil) => ~"closure~",
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn parameterized(cx: ctxt,
|
|
|
|
base: &str,
|
|
|
|
self_r: Option<ty::Region>,
|
|
|
|
tps: &[ty::t]) -> ~str {
|
2012-04-23 18:01:03 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let r_str = match self_r {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ~"",
|
|
|
|
Some(r) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("/%s", region_to_str(cx, r))
|
2012-04-23 18:01:03 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if vec::len(tps) > 0u {
|
2012-09-21 20:43:30 -05:00
|
|
|
let strs = vec::map(tps, |t| ty_to_str(cx, *t));
|
2013-01-05 21:33:37 -06:00
|
|
|
fmt!("%s%s<%s>", base, r_str, str::connect(strs, ","))
|
2012-04-23 18:01:03 -05:00
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%s%s", base, r_str)
|
2012-04-23 18:01:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 17:48:50 -06:00
|
|
|
pub fn ty_to_short_str(cx: ctxt, typ: t) -> ~str {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut s = encoder::encoded_ty(cx, typ);
|
2013-03-21 06:36:21 -05:00
|
|
|
if str::len(s) >= 32u { s = str::slice(s, 0u, 32u).to_owned(); }
|
2012-08-01 19:30:05 -05:00
|
|
|
return s;
|
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:
|