Skip to content

Commit 43ce383

Browse files
committed
Store item paths in ast_map, get rid of trans::local_ctxt
The direct motivation for this was that the monomorphizer needs to be able to generate sane symbols for random items. The typechecker can probably also use this in the future to provide more useful error messages.
1 parent c1b075d commit 43ce383

File tree

18 files changed

+383
-400
lines changed

18 files changed

+383
-400
lines changed

src/comp/back/link.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import syntax::ast;
1818
import syntax::print::pprust;
1919
import lib::llvm::{ModuleRef, mk_pass_manager, mk_target_data, True, False};
2020
import util::filesearch;
21+
import middle::ast_map::{path, path_mod, path_name};
2122

2223
enum output_type {
2324
output_type_none,
@@ -514,25 +515,27 @@ fn get_symbol_hash(ccx: @crate_ctxt, t: ty::t) -> str {
514515
ret hash;
515516
}
516517

517-
fn mangle(ss: [str]) -> str {
518+
fn mangle(ss: path) -> str {
518519
// Follow C++ namespace-mangling style
519520

520521
let n = "_ZN"; // Begin name-sequence.
521522

522-
for s: str in ss { n += #fmt["%u%s", str::byte_len(s), s]; }
523+
for s in ss {
524+
alt s { path_name(s) | path_mod(s) {
525+
n += #fmt["%u%s", str::byte_len(s), s];
526+
} }
527+
}
523528
n += "E"; // End name-sequence.
524-
525-
ret n;
529+
n
526530
}
527531

528-
fn exported_name(path: [str], hash: str, _vers: str) -> str {
532+
fn exported_name(path: path, hash: str, _vers: str) -> str {
529533
// FIXME: versioning isn't working yet
530-
531-
ret mangle(path + [hash]); // + "@" + vers;
534+
ret mangle(path + [path_name(hash)]); // + "@" + vers;
532535

533536
}
534537

535-
fn mangle_exported_name(ccx: @crate_ctxt, path: [str], t: ty::t) -> str {
538+
fn mangle_exported_name(ccx: @crate_ctxt, path: path, t: ty::t) -> str {
536539
let hash = get_symbol_hash(ccx, t);
537540
ret exported_name(path, hash, ccx.link_meta.vers);
538541
}
@@ -541,15 +544,15 @@ fn mangle_internal_name_by_type_only(ccx: @crate_ctxt, t: ty::t, name: str) ->
541544
str {
542545
let s = util::ppaux::ty_to_short_str(ccx.tcx, t);
543546
let hash = get_symbol_hash(ccx, t);
544-
ret mangle([name, s, hash]);
547+
ret mangle([path_name(name), path_name(s), path_name(hash)]);
545548
}
546549

547-
fn mangle_internal_name_by_path_and_seq(ccx: @crate_ctxt, path: [str],
550+
fn mangle_internal_name_by_path_and_seq(ccx: @crate_ctxt, path: path,
548551
flav: str) -> str {
549-
ret mangle(path + [ccx.names(flav)]);
552+
ret mangle(path + [path_name(ccx.names(flav))]);
550553
}
551554

552-
fn mangle_internal_name_by_path(_ccx: @crate_ctxt, path: [str]) -> str {
555+
fn mangle_internal_name_by_path(_ccx: @crate_ctxt, path: path) -> str {
553556
ret mangle(path);
554557
}
555558

src/comp/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,11 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
447447
encode_info_for_mod(ecx, ebml_w, crate_mod, crate_node_id, "");
448448
ecx.ccx.ast_map.items {|key, val|
449449
alt val {
450-
middle::ast_map::node_item(i) {
450+
middle::ast_map::node_item(i, _) {
451451
index += [{val: key, pos: ebml_w.writer.tell()}];
452452
encode_info_for_item(ecx, ebml_w, i, index);
453453
}
454-
middle::ast_map::node_native_item(i) {
454+
middle::ast_map::node_native_item(i, _) {
455455
index += [{val: key, pos: ebml_w.writer.tell()}];
456456
encode_info_for_native_item(ecx, ebml_w, i);
457457
}

src/comp/middle/ast_map.rs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import syntax::ast::*;
44
import syntax::ast_util;
55
import syntax::{visit, codemap};
66

7+
enum path_elt { path_mod(str), path_name(str) }
8+
type path = [path_elt];
9+
710
enum ast_node {
8-
node_item(@item),
9-
node_native_item(@native_item),
10-
node_method(@method),
11+
node_item(@item, @path),
12+
node_native_item(@native_item, @path),
13+
node_method(@method, @path),
1114
node_expr(@expr),
1215
// Locals are numbered, because the alias analysis needs to know in which
1316
// order they are introduced.
@@ -17,66 +20,78 @@ enum ast_node {
1720
}
1821

1922
type map = std::map::map<node_id, ast_node>;
20-
type ctx = @{map: map, mutable local_id: uint};
23+
type ctx = {map: map, mutable path: path, mutable local_id: uint};
24+
type vt = visit::vt<ctx>;
2125

2226
fn map_crate(c: crate) -> map {
23-
let cx = @{map: std::map::new_int_hash(),
24-
mutable local_id: 0u};
25-
26-
let v_map = visit::mk_simple_visitor
27-
(@{visit_item: bind map_item(cx, _),
28-
visit_native_item: bind map_native_item(cx, _),
29-
visit_expr: bind map_expr(cx, _),
30-
visit_fn: bind map_fn(cx, _, _, _, _, _),
31-
visit_local: bind map_local(cx, _),
32-
visit_arm: bind map_arm(cx, _)
33-
with *visit::default_simple_visitor()});
34-
visit::visit_crate(c, (), v_map);
27+
let cx = {map: std::map::new_int_hash(),
28+
mutable path: [],
29+
mutable local_id: 0u};
30+
visit::visit_crate(c, cx, visit::mk_vt(@{
31+
visit_item: map_item,
32+
visit_native_item: map_native_item,
33+
visit_expr: map_expr,
34+
visit_fn: map_fn,
35+
visit_local: map_local,
36+
visit_arm: map_arm
37+
with *visit::default_visitor()
38+
}));
3539
ret cx.map;
3640
}
3741

38-
fn map_fn(cx: ctx, _fk: visit::fn_kind, decl: fn_decl, _body: blk,
39-
_sp: codemap::span, _id: node_id) {
42+
fn map_fn(fk: visit::fn_kind, decl: fn_decl, body: blk,
43+
sp: codemap::span, id: node_id, cx: ctx, v: vt) {
4044
for a in decl.inputs {
4145
cx.map.insert(a.id, node_arg(a, cx.local_id));
4246
cx.local_id += 1u;
4347
}
48+
visit::visit_fn(fk, decl, body, sp, id, cx, v);
4449
}
4550

46-
fn map_local(cx: ctx, loc: @local) {
51+
fn map_local(loc: @local, cx: ctx, v: vt) {
4752
pat_util::pat_bindings(loc.node.pat) {|p_id, _s, _p|
4853
cx.map.insert(p_id, node_local(cx.local_id));
4954
cx.local_id += 1u;
5055
};
56+
visit::visit_local(loc, cx, v);
5157
}
5258

53-
fn map_arm(cx: ctx, arm: arm) {
59+
fn map_arm(arm: arm, cx: ctx, v: vt) {
5460
pat_util::pat_bindings(arm.pats[0]) {|p_id, _s, _p|
5561
cx.map.insert(p_id, node_local(cx.local_id));
5662
cx.local_id += 1u;
5763
};
64+
visit::visit_arm(arm, cx, v);
5865
}
5966

60-
fn map_item(cx: ctx, i: @item) {
61-
cx.map.insert(i.id, node_item(i));
67+
fn map_item(i: @item, cx: ctx, v: vt) {
68+
cx.map.insert(i.id, node_item(i, @cx.path));
6269
alt i.node {
6370
item_impl(_, _, _, ms) {
64-
for m in ms { cx.map.insert(m.id, node_method(m)); }
71+
for m in ms { cx.map.insert(m.id, node_method(m, @cx.path)); }
6572
}
6673
item_res(_, _, _, dtor_id, ctor_id) {
6774
cx.map.insert(ctor_id, node_res_ctor(i));
68-
cx.map.insert(dtor_id, node_item(i));
75+
cx.map.insert(dtor_id, node_item(i, @cx.path));
6976
}
7077
_ { }
7178
}
79+
alt i.node {
80+
item_mod(_) | item_native_mod(_) { cx.path += [path_mod(i.ident)]; }
81+
_ { cx.path += [path_name(i.ident)]; }
82+
}
83+
visit::visit_item(i, cx, v);
84+
vec::pop(cx.path);
7285
}
7386

74-
fn map_native_item(cx: ctx, i: @native_item) {
75-
cx.map.insert(i.id, node_native_item(i));
87+
fn map_native_item(i: @native_item, cx: ctx, v: vt) {
88+
cx.map.insert(i.id, node_native_item(i, @cx.path));
89+
visit::visit_native_item(i, cx, v);
7690
}
7791

78-
fn map_expr(cx: ctx, ex: @expr) {
92+
fn map_expr(ex: @expr, cx: ctx, v: vt) {
7993
cx.map.insert(ex.id, node_expr(ex));
94+
visit::visit_expr(ex, cx, v);
8095
}
8196

8297
// Local Variables:

src/comp/middle/debuginfo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ fn create_function(fcx: @fn_ctxt) -> @metadata<subprogram_md> {
761761
log(debug, codemap::span_to_str(sp, cx.sess.codemap));
762762

763763
let (ident, ret_ty, id) = alt cx.ast_map.get(fcx.id) {
764-
ast_map::node_item(item) {
764+
ast_map::node_item(item, _) {
765765
alt item.node {
766766
ast::item_fn(decl, _, _) | ast::item_res(decl, _, _, _, _) {
767767
(item.ident, decl.output, item.id)
@@ -770,7 +770,7 @@ fn create_function(fcx: @fn_ctxt) -> @metadata<subprogram_md> {
770770
bound to non-function"); }
771771
}
772772
}
773-
ast_map::node_method(method) {
773+
ast_map::node_method(method, _) {
774774
(method.ident, method.decl.output, method.id)
775775
}
776776
ast_map::node_res_ctor(item) {
@@ -808,7 +808,7 @@ fn create_function(fcx: @fn_ctxt) -> @metadata<subprogram_md> {
808808
option::none {}
809809
}
810810

811-
let path = str::connect(fcx.lcx.path + [ident], "::");
811+
let path = path_str(fcx.path);
812812

813813
let loc = codemap::lookup_char_pos(cx.sess.codemap,
814814
sp.lo);

src/comp/middle/resolve.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ fn visit_fn_with_scope(e: @env, fk: visit::fn_kind, decl: ast::fn_decl,
480480
// is this a main fn declaration?
481481
alt fk {
482482
visit::fk_item_fn(nm, _) {
483-
if is_main_name([nm]) && !e.sess.building_library {
483+
if is_main_name([ast_map::path_name(nm)]) &&
484+
!e.sess.building_library {
484485
// This is a main function -- set it in the session
485486
// as the main ID
486487
e.sess.main_fn = some((id, sp));

src/comp/middle/shape.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,6 @@ fn shape_of_variant(ccx: @crate_ctxt, v: ty::variant_info,
470470
ret s;
471471
}
472472

473-
//fn variant_names(ccx: @crate_ctxt, tag_id: ast::def_id) -> [str] {
474-
// assert ast::local_crate == tag_id.crate;
475-
// alt ccx.tcx.items.get(tag_id.node) {
476-
// ast_map::node_item(@{node: ast::item_tag(variants, _), _}) {
477-
// vec::map(variants) {|variant| variant.node.name}
478-
// }
479-
// }
480-
//}
481-
482473
fn gen_enum_shapes(ccx: @crate_ctxt) -> ValueRef {
483474
// Loop over all the enum variants and write their shapes into a
484475
// data buffer. As we do this, it's possible for us to discover

src/comp/middle/trans/alt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn get_options(ccx: @crate_ctxt, m: match, col: uint) -> [opt] {
265265
fn extract_variant_args(bcx: @block_ctxt, pat_id: ast::node_id,
266266
vdefs: {enm: def_id, var: def_id}, val: ValueRef) ->
267267
{vals: [ValueRef], bcx: @block_ctxt} {
268-
let ccx = bcx.fcx.lcx.ccx, bcx = bcx;
268+
let ccx = bcx.fcx.ccx, bcx = bcx;
269269
// invariant:
270270
// pat_id must have the same length ty_param_substs as vdefs?
271271
let ty_param_substs = ty::node_id_to_type_params(ccx.tcx, pat_id);
@@ -412,7 +412,7 @@ fn compile_submatch(bcx: @block_ctxt, m: match, vals: [ValueRef], f: mk_fail,
412412
let vals_left =
413413
vec::slice(vals, 0u, col) +
414414
vec::slice(vals, col + 1u, vec::len(vals));
415-
let ccx = bcx.fcx.lcx.ccx;
415+
let ccx = bcx.fcx.ccx;
416416
let pat_id = 0;
417417
for br: match_branch in m {
418418
// Find a real id (we're adding placeholder wildcard patterns, but
@@ -692,7 +692,7 @@ fn trans_alt(cx: @block_ctxt, expr: @ast::expr, arms_: [ast::arm],
692692
}
693693

694694
let exit_map = [];
695-
let t = base::node_id_type(cx.fcx.lcx.ccx, expr.id);
695+
let t = base::node_id_type(cx.fcx.ccx, expr.id);
696696
let vr = base::spill_if_immediate(er.bcx, er.val, t);
697697
compile_submatch(vr.bcx, match, [vr.val],
698698
bind mk_fail(alt_cx, expr.span, fail_cx), exit_map);
@@ -719,7 +719,7 @@ fn trans_alt(cx: @block_ctxt, expr: @ast::expr, arms_: [ast::arm],
719719
// Not alt-related, but similar to the pattern-munging code above
720720
fn bind_irrefutable_pat(bcx: @block_ctxt, pat: @ast::pat, val: ValueRef,
721721
make_copy: bool) -> @block_ctxt {
722-
let ccx = bcx.fcx.lcx.ccx, bcx = bcx;
722+
let ccx = bcx.fcx.ccx, bcx = bcx;
723723

724724
// Necessary since bind_irrefutable_pat is called outside trans_alt
725725
alt normalize_pat(bcx_tcx(bcx), pat).node {

0 commit comments

Comments
 (0)