Skip to content

Commit daff545

Browse files
marijnhgraydon
authored andcommitted
---
yaml --- r: 2020 b: refs/heads/master c: 39774e8 h: refs/heads/master v: v3
1 parent 5ae190f commit daff545

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: d678dad434d85c25457fc8c234386f65fae4f07b
2+
refs/heads/master: 39774e88b42409c3a432a8426cfef253f67d7709

trunk/src/comp/driver/rustc.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn usage(session.session sess, str argv0) {
9898
log " -glue generate glue.bc file";
9999
log " -shared compile a shared-library crate";
100100
log " -pp pretty-print the input instead of compiling";
101+
log " -ls list the symbols defined by a crate file";
101102
log " -L <path> add a directory to the library search path";
102103
log " -h display this message";
103104
log "";
@@ -130,6 +131,7 @@ impure fn main(vec[str] args) {
130131
let bool do_warn = true;
131132
let bool shared = false;
132133
let bool pretty = false;
134+
let bool ls = false;
133135
let bool glue = false;
134136

135137
// FIXME: Maybe we should support -O0, -O1, -Os, etc
@@ -152,6 +154,8 @@ impure fn main(vec[str] args) {
152154
shared = true;
153155
} else if (_str.eq(arg, "-pp")) {
154156
pretty = true;
157+
} else if (_str.eq(arg, "-ls")) {
158+
ls = true;
155159
} else if (_str.eq(arg, "-o")) {
156160
if (i+1u < len) {
157161
output_file = some(args.(i+1u));
@@ -214,8 +218,9 @@ impure fn main(vec[str] args) {
214218
auto env = default_environment(sess, args.(0), ifile);
215219
if (pretty) {
216220
pretty_print_input(sess, env, ifile);
217-
}
218-
else {
221+
} else if (ls) {
222+
front.creader.list_file_metadata(ifile, std.io.stdout());
223+
} else {
219224
alt (output_file) {
220225
case (none[str]) {
221226
let vec[str] parts = _str.split(ifile, '.' as u8);

trunk/src/comp/front/creader.rs

+66
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,72 @@ fn get_tag_variants(session.session sess, ast.def_id def)
566566
ret infos;
567567
}
568568

569+
impure fn list_file_metadata(str path, io.writer out) {
570+
alt (get_metadata_section(path)) {
571+
case (option.some[vec[u8]](?bytes)) {
572+
list_crate_metadata(bytes, out);
573+
}
574+
case (option.none[vec[u8]]) {
575+
out.write_str("Could not find metadata in " + path + ".\n");
576+
}
577+
}
578+
}
579+
580+
fn read_path(&ebml.doc d) -> tup(str, uint) {
581+
auto desc = ebml.doc_data(d);
582+
auto pos = ebml.be_uint_from_bytes(desc, 0u, 4u);
583+
auto pathbytes = _vec.slice[u8](desc, 4u, _vec.len[u8](desc));
584+
auto path = _str.unsafe_from_bytes(pathbytes);
585+
ret tup(path, pos);
586+
}
587+
588+
impure fn list_crate_metadata(vec[u8] bytes, io.writer out) {
589+
auto md = ebml.new_doc(bytes);
590+
auto paths = ebml.get_doc(md, metadata.tag_paths);
591+
auto items = ebml.get_doc(md, metadata.tag_items);
592+
auto index = ebml.get_doc(paths, metadata.tag_index);
593+
auto bs = ebml.get_doc(index, metadata.tag_index_buckets);
594+
for each (ebml.doc bucket in
595+
ebml.tagged_docs(bs, metadata.tag_index_buckets_bucket)) {
596+
auto et = metadata.tag_index_buckets_bucket_elt;
597+
for each (ebml.doc elt in ebml.tagged_docs(bucket, et)) {
598+
auto data = read_path(elt);
599+
auto def = ebml.doc_at(bytes, data._1);
600+
auto did_doc = ebml.get_doc(def, metadata.tag_def_id);
601+
auto did = parse_def_id(ebml.doc_data(did_doc));
602+
out.write_str(#fmt("%s (%s)\n", data._0,
603+
describe_def(items, did)));
604+
}
605+
}
606+
}
607+
608+
fn describe_def(&ebml.doc items, ast.def_id id) -> str {
609+
if (id._0 != 0) {ret "external";}
610+
alt (maybe_find_item(id._1 as int, items)) {
611+
case (option.some[ebml.doc](?item)) {
612+
ret item_kind_to_str(item_kind(item));
613+
}
614+
case (option.none[ebml.doc]) {
615+
ret "??"; // Native modules don't seem to get item entries.
616+
}
617+
}
618+
}
619+
620+
fn item_kind_to_str(u8 kind) -> str {
621+
alt (kind as char) {
622+
case ('c') {ret "const";}
623+
case ('f') {ret "fn";}
624+
case ('F') {ret "native fn";}
625+
case ('y') {ret "type";}
626+
case ('o') {ret "obj";}
627+
case ('T') {ret "native type";}
628+
case ('t') {ret "type";}
629+
case ('m') {ret "mod";}
630+
case ('n') {ret "native mod";}
631+
case ('v') {ret "tag";}
632+
}
633+
}
634+
569635
// Local Variables:
570636
// mode: rust
571637
// fill-column: 78;

0 commit comments

Comments
 (0)