rustc: Add a typed pretty-printing mode for debugging

This commit is contained in:
Patrick Walton 2011-05-17 17:42:23 -07:00
parent 376b087f3f
commit 7a3812afc3
2 changed files with 60 additions and 14 deletions

View File

@ -10,6 +10,7 @@
import middle::ty;
import middle::typeck;
import middle::tstate::ck;
import pretty::pprust;
import back::link;
import lib::llvm;
import util::common;
@ -117,14 +118,26 @@ fn compile_input(session::session sess,
bind link::write::run_passes(sess, llmod, output));
}
fn pretty_print_input(session::session sess,
eval::env env,
str input) {
fn pretty_print_input(session::session sess, eval::env env, str input,
bool typed) {
auto def = tup(ast::local_crate, 0);
auto p = front::parser::new_parser(sess, env, def, input, 0u, 0u);
auto crate = front::parser::parse_crate_from_source_file(p);
pretty::pprust::print_file(sess, crate.node.module, input,
std::io::stdout());
auto mode;
if (typed) {
crate = creader::read_crates(sess, crate);
auto def_map = resolve::resolve_crate(sess, crate);
auto ty_cx = ty::mk_ctxt(sess, def_map);
auto typeck_result = typeck::check_crate(ty_cx, crate);
crate = typeck_result._2;
mode = pprust::mo_typed(ty_cx, typeck_result._0, typeck_result._1);
} else {
mode = pprust::mo_untyped;
}
pprust::print_file(sess, crate.node.module, input, std::io::stdout(),
mode);
}
fn version(str argv0) {
@ -147,6 +160,7 @@ fn usage(str argv0) {
--glue generate glue.bc file
--shared compile a shared-library crate
--pretty pretty-print the input instead of compiling
--typed-pretty pretty-print the input with types instead of compiling
--ls list the symbols defined by a crate file
-L <path> add a directory to the library search path
--noverify suppress LLVM verification step (slight speedup)
@ -214,7 +228,8 @@ fn main(vec[str] args) {
auto opts = [optflag("h"), optflag("help"),
optflag("v"), optflag("version"),
optflag("glue"), optflag("emit-llvm"),
optflag("pretty"), optflag("ls"), optflag("parse-only"),
optflag("pretty"), optflag("typed-pretty"),
optflag("ls"), optflag("parse-only"),
optflag("O"), optflag("shared"), optmulti("L"),
optflag("S"), optflag("c"), optopt("o"), optflag("g"),
optflag("save-temps"), optopt("sysroot"),
@ -243,6 +258,7 @@ fn main(vec[str] args) {
}
auto pretty = opt_present(match, "pretty");
auto typed_pretty = opt_present(match, "typed-pretty");
auto ls = opt_present(match, "ls");
auto glue = opt_present(match, "glue");
auto shared = opt_present(match, "shared");
@ -318,8 +334,8 @@ fn main(vec[str] args) {
auto ifile = match.free.(0);
let str saved_out_filename = "";
auto env = default_environment(sess, args.(0), ifile);
if (pretty) {
pretty_print_input(sess, env, ifile);
if (pretty || typed_pretty) {
pretty_print_input(sess, env, ifile, typed_pretty);
} else if (ls) {
front::creader::list_file_metadata(ifile, std::io::stdout());
} else {

View File

@ -5,21 +5,30 @@
import driver::session::session;
import front::ast;
import front::lexer;
import middle::ty;
import util::common;
import pp::end; import pp::wrd; import pp::space; import pp::line;
const uint indent_unit = 4u;
const uint default_columns = 78u;
tag mode {
mo_untyped;
mo_typed(ty::ctxt, ty::node_type_table, ty::type_cache);
}
type ps = @rec(pp::ps s,
option::t[vec[lexer::cmnt]] comments,
mutable uint cur_cmnt);
mutable uint cur_cmnt,
mode mode);
fn print_file(session sess, ast::_mod _mod, str filename, io::writer out) {
fn print_file(session sess, ast::_mod _mod, str filename, io::writer out,
mode mode) {
auto cmnts = lexer::gather_comments(sess, filename);
auto s = @rec(s=pp::mkstate(out, default_columns),
comments=option::some[vec[lexer::cmnt]](cmnts),
mutable cur_cmnt=0u);
mutable cur_cmnt=0u,
mode=mode);
print_mod(s, _mod);
}
@ -27,7 +36,8 @@ fn ty_to_str(&@ast::ty ty) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mkstate(writer.get_writer(), 0u),
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u);
mutable cur_cmnt=0u,
mode=mo_untyped);
print_type(s, ty);
ret writer.get_str();
}
@ -36,7 +46,8 @@ fn block_to_str(&ast::block blk) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u),
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u);
mutable cur_cmnt=0u,
mode=mo_untyped);
print_block(s, blk);
ret writer.get_str();
}
@ -45,7 +56,8 @@ fn pat_to_str(&@ast::pat p) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u),
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u);
mutable cur_cmnt=0u,
mode=mo_untyped);
print_pat(s, p);
ret writer.get_str();
}
@ -391,6 +403,12 @@ fn print_literal(ps s, @ast::lit lit) {
fn print_expr(ps s, &@ast::expr expr) {
maybe_print_comment(s, expr.span.lo);
hbox(s);
alt (s.mode) {
case (mo_untyped) { /* no-op */ }
case (mo_typed(_, _, _)) { popen(s); }
}
alt (expr.node) {
case (ast::expr_vec(?exprs,?mut,_)) {
if (mut == ast::mut) {
@ -697,6 +715,18 @@ fn print_opt(ps s, &option::t[@ast::expr] expr) {
// TODO
}
}
// Print the type if necessary.
alt (s.mode) {
case (mo_untyped) { /* no-op */ }
case (mo_typed(?tcx, ?ntt, ?tc)) {
space(s.s);
wrd1(s, "as");
wrd(s.s, ty::ty_to_str(tcx, ty::expr_ty(tcx, ntt, expr)));
pclose(s);
}
}
end(s.s);
}