Skip to content

Commit 1af3174

Browse files
committed
Move to single-uint file-position representation.
This makes passing them around cheaper. There is now a table (see front/codemap.rs) that is needed to transform such an uint into an actual filename/line/col location. Also cleans up the span building in the parser a bit.
1 parent 094d31f commit 1af3174

File tree

11 files changed

+413
-351
lines changed

11 files changed

+413
-351
lines changed

src/comp/driver/rustc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impure fn compile_input(session.session sess,
6262
bool parse_only,
6363
vec[str] library_search_paths) {
6464
auto def = tup(0, 0);
65-
auto p = parser.new_parser(sess, env, def, input);
65+
auto p = parser.new_parser(sess, env, def, input, 0u);
6666
auto crate = parse_input(sess, p, input);
6767
if (parse_only) {ret;}
6868
crate = creader.read_crates(sess, crate, library_search_paths);
@@ -79,7 +79,7 @@ impure fn pretty_print_input(session.session sess,
7979
eval.env env,
8080
str input) {
8181
auto def = tup(0, 0);
82-
auto p = front.parser.new_parser(sess, env, def, input);
82+
auto p = front.parser.new_parser(sess, env, def, input, 0u);
8383
auto crate = front.parser.parse_crate_from_source_file(p);
8484
pretty.pprust.print_file(crate.node.module, input, std.io.stdout());
8585
}
@@ -125,7 +125,8 @@ impure fn main(vec[str] args) {
125125

126126
auto crate_cache = common.new_int_hash[session.crate_metadata]();
127127
auto target_crate_num = 0;
128-
auto sess = session.session(target_crate_num, target_cfg, crate_cache);
128+
auto sess = session.session(target_crate_num, target_cfg, crate_cache,
129+
front.codemap.new_codemap());
129130

130131
let option.t[str] input_file = none[str];
131132
let option.t[str] output_file = none[str];

src/comp/driver/session.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import front.ast;
2+
import front.codemap;
23
import util.common.span;
34
import util.common.ty_mach;
45
import std._uint;
@@ -25,7 +26,8 @@ type cfg = rec(os os,
2526
type crate_metadata = vec[u8];
2627

2728
obj session(ast.crate_num cnum, cfg targ,
28-
map.hashmap[int, crate_metadata] crates) {
29+
map.hashmap[int, crate_metadata] crates,
30+
codemap.codemap cm) {
2931

3032
fn get_targ_cfg() -> cfg {
3133
ret targ;
@@ -36,10 +38,12 @@ obj session(ast.crate_num cnum, cfg targ,
3638
}
3739

3840
fn span_err(span sp, str msg) {
41+
auto lo = codemap.lookup_pos(cm, sp.lo);
42+
auto hi = codemap.lookup_pos(cm, sp.hi);
3943
log #fmt("%s:%u:%u:%u:%u: error: %s",
40-
sp.filename,
41-
sp.lo.line, sp.lo.col,
42-
sp.hi.line, sp.hi.col,
44+
lo.filename,
45+
lo.line, lo.col,
46+
hi.line, hi.col,
4347
msg);
4448
fail;
4549
}
@@ -50,10 +54,12 @@ obj session(ast.crate_num cnum, cfg targ,
5054
}
5155

5256
fn span_warn(span sp, str msg) {
57+
auto lo = codemap.lookup_pos(cm, sp.lo);
58+
auto hi = codemap.lookup_pos(cm, sp.hi);
5359
log #fmt("%s:%u:%u:%u:%u: warning: %s",
54-
sp.filename,
55-
sp.lo.line, sp.lo.col,
56-
sp.hi.line, sp.hi.col,
60+
lo.filename,
61+
lo.line, lo.col,
62+
hi.line, hi.col,
5763
msg);
5864
}
5965

@@ -63,10 +69,12 @@ obj session(ast.crate_num cnum, cfg targ,
6369
}
6470

6571
fn span_unimpl(span sp, str msg) {
72+
auto lo = codemap.lookup_pos(cm, sp.lo);
73+
auto hi = codemap.lookup_pos(cm, sp.hi);
6674
log #fmt("%s:%u:%u:%u:%u: error: unimplemented %s",
67-
sp.filename,
68-
sp.lo.line, sp.lo.col,
69-
sp.hi.line, sp.hi.col,
75+
lo.filename,
76+
lo.line, lo.col,
77+
hi.line, hi.col,
7078
msg);
7179
fail;
7280
}
@@ -87,6 +95,14 @@ obj session(ast.crate_num cnum, cfg targ,
8795
fn has_external_crate(int num) -> bool {
8896
ret crates.contains_key(num);
8997
}
98+
99+
fn get_codemap() -> codemap.codemap {
100+
ret cm;
101+
}
102+
103+
fn lookup_pos(uint pos) -> codemap.loc {
104+
ret codemap.lookup_pos(cm, pos);
105+
}
90106
}
91107

92108

src/comp/front/codemap.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import std._vec;
2+
3+
/* A codemap is a thing that maps uints to file/line/column positions
4+
* in a crate. This to make it possible to represent the positions
5+
* with single-word things, rather than passing records all over the
6+
* compiler.
7+
*/
8+
9+
type filemap = @rec(str name,
10+
uint start_pos,
11+
mutable vec[uint] lines);
12+
type codemap = @rec(mutable vec[filemap] files);
13+
type loc = rec(str filename, uint line, uint col);
14+
15+
fn new_codemap() -> codemap {
16+
let vec[filemap] files = vec();
17+
ret @rec(mutable files=files);
18+
}
19+
20+
fn new_filemap(str filename, uint start_pos) -> filemap {
21+
let vec[uint] lines = vec();
22+
ret @rec(name=filename,
23+
start_pos=start_pos,
24+
mutable lines=lines);
25+
}
26+
27+
fn next_line(filemap file, uint pos) {
28+
_vec.push[uint](file.lines, pos);
29+
}
30+
31+
fn lookup_pos(codemap map, uint pos) -> loc {
32+
for (filemap f in map.files) {
33+
if (f.start_pos < pos) {
34+
auto line_num = 1u;
35+
auto line_start = 0u;
36+
// FIXME this can be a binary search if we need to be faster
37+
for (uint line_start_ in f.lines) {
38+
// FIXME duplicate code due to lack of working break
39+
if (line_start_ > pos) {
40+
ret rec(filename=f.name,
41+
line=line_num,
42+
col=pos-line_start);
43+
}
44+
line_start = line_start_;
45+
line_num += 1u;
46+
}
47+
ret rec(filename=f.name,
48+
line=line_num,
49+
col=pos-line_start);
50+
}
51+
}
52+
log #fmt("Failed to find a location for character %u", pos);
53+
fail;
54+
}
55+
56+
//
57+
// Local Variables:
58+
// mode: rust
59+
// fill-column: 78;
60+
// indent-tabs-mode: nil
61+
// c-basic-offset: 4
62+
// buffer-file-coding-system: utf-8-unix
63+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
64+
// End:
65+
//

0 commit comments

Comments
 (0)