Skip to content

Commit 7a3812a

Browse files
committed
rustc: Add a typed pretty-printing mode for debugging
1 parent 376b087 commit 7a3812a

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

src/comp/driver/rustc.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import middle::resolve;
1010
import middle::ty;
1111
import middle::typeck;
1212
import middle::tstate::ck;
13+
import pretty::pprust;
1314
import back::link;
1415
import lib::llvm;
1516
import util::common;
@@ -117,14 +118,26 @@ fn compile_input(session::session sess,
117118
bind link::write::run_passes(sess, llmod, output));
118119
}
119120

120-
fn pretty_print_input(session::session sess,
121-
eval::env env,
122-
str input) {
121+
fn pretty_print_input(session::session sess, eval::env env, str input,
122+
bool typed) {
123123
auto def = tup(ast::local_crate, 0);
124124
auto p = front::parser::new_parser(sess, env, def, input, 0u, 0u);
125125
auto crate = front::parser::parse_crate_from_source_file(p);
126-
pretty::pprust::print_file(sess, crate.node.module, input,
127-
std::io::stdout());
126+
127+
auto mode;
128+
if (typed) {
129+
crate = creader::read_crates(sess, crate);
130+
auto def_map = resolve::resolve_crate(sess, crate);
131+
auto ty_cx = ty::mk_ctxt(sess, def_map);
132+
auto typeck_result = typeck::check_crate(ty_cx, crate);
133+
crate = typeck_result._2;
134+
mode = pprust::mo_typed(ty_cx, typeck_result._0, typeck_result._1);
135+
} else {
136+
mode = pprust::mo_untyped;
137+
}
138+
139+
pprust::print_file(sess, crate.node.module, input, std::io::stdout(),
140+
mode);
128141
}
129142

130143
fn version(str argv0) {
@@ -147,6 +160,7 @@ options:
147160
--glue generate glue.bc file
148161
--shared compile a shared-library crate
149162
--pretty pretty-print the input instead of compiling
163+
--typed-pretty pretty-print the input with types instead of compiling
150164
--ls list the symbols defined by a crate file
151165
-L <path> add a directory to the library search path
152166
--noverify suppress LLVM verification step (slight speedup)
@@ -214,7 +228,8 @@ fn main(vec[str] args) {
214228
auto opts = [optflag("h"), optflag("help"),
215229
optflag("v"), optflag("version"),
216230
optflag("glue"), optflag("emit-llvm"),
217-
optflag("pretty"), optflag("ls"), optflag("parse-only"),
231+
optflag("pretty"), optflag("typed-pretty"),
232+
optflag("ls"), optflag("parse-only"),
218233
optflag("O"), optflag("shared"), optmulti("L"),
219234
optflag("S"), optflag("c"), optopt("o"), optflag("g"),
220235
optflag("save-temps"), optopt("sysroot"),
@@ -243,6 +258,7 @@ fn main(vec[str] args) {
243258
}
244259

245260
auto pretty = opt_present(match, "pretty");
261+
auto typed_pretty = opt_present(match, "typed-pretty");
246262
auto ls = opt_present(match, "ls");
247263
auto glue = opt_present(match, "glue");
248264
auto shared = opt_present(match, "shared");
@@ -318,8 +334,8 @@ fn main(vec[str] args) {
318334
auto ifile = match.free.(0);
319335
let str saved_out_filename = "";
320336
auto env = default_environment(sess, args.(0), ifile);
321-
if (pretty) {
322-
pretty_print_input(sess, env, ifile);
337+
if (pretty || typed_pretty) {
338+
pretty_print_input(sess, env, ifile, typed_pretty);
323339
} else if (ls) {
324340
front::creader::list_file_metadata(ifile, std::io::stdout());
325341
} else {

src/comp/pretty/pprust.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@ import std::option;
55
import driver::session::session;
66
import front::ast;
77
import front::lexer;
8+
import middle::ty;
89
import util::common;
910
import pp::end; import pp::wrd; import pp::space; import pp::line;
1011

1112
const uint indent_unit = 4u;
1213
const uint default_columns = 78u;
1314

15+
tag mode {
16+
mo_untyped;
17+
mo_typed(ty::ctxt, ty::node_type_table, ty::type_cache);
18+
}
19+
1420
type ps = @rec(pp::ps s,
1521
option::t[vec[lexer::cmnt]] comments,
16-
mutable uint cur_cmnt);
22+
mutable uint cur_cmnt,
23+
mode mode);
1724

18-
fn print_file(session sess, ast::_mod _mod, str filename, io::writer out) {
25+
fn print_file(session sess, ast::_mod _mod, str filename, io::writer out,
26+
mode mode) {
1927
auto cmnts = lexer::gather_comments(sess, filename);
2028
auto s = @rec(s=pp::mkstate(out, default_columns),
2129
comments=option::some[vec[lexer::cmnt]](cmnts),
22-
mutable cur_cmnt=0u);
30+
mutable cur_cmnt=0u,
31+
mode=mode);
2332
print_mod(s, _mod);
2433
}
2534

2635
fn ty_to_str(&@ast::ty ty) -> str {
2736
auto writer = io::string_writer();
2837
auto s = @rec(s=pp::mkstate(writer.get_writer(), 0u),
2938
comments=option::none[vec[lexer::cmnt]],
30-
mutable cur_cmnt=0u);
39+
mutable cur_cmnt=0u,
40+
mode=mo_untyped);
3141
print_type(s, ty);
3242
ret writer.get_str();
3343
}
@@ -36,7 +46,8 @@ fn block_to_str(&ast::block blk) -> str {
3646
auto writer = io::string_writer();
3747
auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u),
3848
comments=option::none[vec[lexer::cmnt]],
39-
mutable cur_cmnt=0u);
49+
mutable cur_cmnt=0u,
50+
mode=mo_untyped);
4051
print_block(s, blk);
4152
ret writer.get_str();
4253
}
@@ -45,7 +56,8 @@ fn pat_to_str(&@ast::pat p) -> str {
4556
auto writer = io::string_writer();
4657
auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u),
4758
comments=option::none[vec[lexer::cmnt]],
48-
mutable cur_cmnt=0u);
59+
mutable cur_cmnt=0u,
60+
mode=mo_untyped);
4961
print_pat(s, p);
5062
ret writer.get_str();
5163
}
@@ -391,6 +403,12 @@ fn print_literal(ps s, @ast::lit lit) {
391403
fn print_expr(ps s, &@ast::expr expr) {
392404
maybe_print_comment(s, expr.span.lo);
393405
hbox(s);
406+
407+
alt (s.mode) {
408+
case (mo_untyped) { /* no-op */ }
409+
case (mo_typed(_, _, _)) { popen(s); }
410+
}
411+
394412
alt (expr.node) {
395413
case (ast::expr_vec(?exprs,?mut,_)) {
396414
if (mut == ast::mut) {
@@ -697,6 +715,18 @@ fn print_expr(ps s, &@ast::expr expr) {
697715
// TODO
698716
}
699717
}
718+
719+
// Print the type if necessary.
720+
alt (s.mode) {
721+
case (mo_untyped) { /* no-op */ }
722+
case (mo_typed(?tcx, ?ntt, ?tc)) {
723+
space(s.s);
724+
wrd1(s, "as");
725+
wrd(s.s, ty::ty_to_str(tcx, ty::expr_ty(tcx, ntt, expr)));
726+
pclose(s);
727+
}
728+
}
729+
700730
end(s.s);
701731
}
702732

0 commit comments

Comments
 (0)