Skip to content

Commit fd465f9

Browse files
committed
Drop collect_items pass, create decls on demand
This solves a problem with inlined functions that have inner functions.
1 parent 6f8fe78 commit fd465f9

File tree

15 files changed

+295
-331
lines changed

15 files changed

+295
-331
lines changed

src/rustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg,
131131

132132
let ast_map =
133133
time(time_passes, "ast indexing",
134-
bind middle::ast_map::map_crate(*crate));
134+
bind middle::ast_map::map_crate(sess, *crate));
135135
time(time_passes, "external crate/lib resolution",
136136
bind creader::read_crates(sess, *crate));
137137
let {def_map, exp_map, impl_map} =

src/rustc/metadata/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn decode_inlined_item(cdata: cstore::crate_metadata,
8989
to_id_range: to_id_range};
9090
let raw_ii = decode_ast(ast_doc);
9191
let ii = renumber_ast(xcx, raw_ii);
92-
ast_map::map_decoded_item(dcx.tcx.items, path, ii);
92+
ast_map::map_decoded_item(tcx.sess, dcx.tcx.items, path, ii);
9393
#debug["Fn named: %s", ii.ident()];
9494
decode_side_tables(xcx, ast_doc);
9595
#debug["< Decoded inlined fn: %s::%s",

src/rustc/metadata/encoder.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn purity_fn_family(p: purity) -> char {
328328
}
329329

330330
fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
331-
&index: [entry<int>], path: ast_map::path) {
331+
&index: [entry<int>], path: ast_map::path) -> bool {
332332

333333
fn should_inline(attrs: [attribute]) -> bool {
334334
alt attr::find_inline_attr(attrs) {
@@ -342,7 +342,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
342342
item_enum(_, _) | item_res(_, _, _, _, _) { true }
343343
_ { false }
344344
};
345-
if !must_write && !ecx.reachable.contains_key(item.id) { ret; }
345+
if !must_write && !ecx.reachable.contains_key(item.id) { ret false; }
346346

347347
alt item.node {
348348
item_const(_, _) {
@@ -494,11 +494,13 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
494494
ebml_w.end_tag();
495495
}
496496
}
497+
ret true;
497498
}
498499

499500
fn encode_info_for_native_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
500-
nitem: @native_item, path: ast_map::path) {
501-
if !ecx.reachable.contains_key(nitem.id) { ret; }
501+
nitem: @native_item, path: ast_map::path)
502+
-> bool {
503+
if !ecx.reachable.contains_key(nitem.id) { ret false; }
502504
ebml_w.start_tag(tag_items_data_item);
503505
alt nitem.node {
504506
native_item_fn(fn_decl, tps) {
@@ -511,6 +513,7 @@ fn encode_info_for_native_item(ecx: @encode_ctxt, ebml_w: ebml::writer,
511513
}
512514
}
513515
ebml_w.end_tag();
516+
ret true;
514517
}
515518

516519
fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
@@ -520,17 +523,17 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: ebml::writer,
520523
index += [{val: crate_node_id, pos: ebml_w.writer.tell()}];
521524
encode_info_for_mod(ecx, ebml_w, crate_mod, crate_node_id, [], "");
522525
ecx.ccx.tcx.items.items {|key, val|
523-
alt val {
526+
let where = ebml_w.writer.tell();
527+
let written = alt val {
524528
middle::ast_map::node_item(i, path) {
525-
index += [{val: key, pos: ebml_w.writer.tell()}];
526-
encode_info_for_item(ecx, ebml_w, i, index, *path);
529+
encode_info_for_item(ecx, ebml_w, i, index, *path)
527530
}
528-
middle::ast_map::node_native_item(i, path) {
529-
index += [{val: key, pos: ebml_w.writer.tell()}];
530-
encode_info_for_native_item(ecx, ebml_w, i, *path);
531+
middle::ast_map::node_native_item(i, _, path) {
532+
encode_info_for_native_item(ecx, ebml_w, i, *path)
531533
}
532-
_ { }
533-
}
534+
_ { false }
535+
};
536+
if written { index += [{val: key, pos: where}]; }
534537
};
535538
ebml_w.end_tag();
536539
ret index;

src/rustc/metadata/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn traverse_def_id(cx: ctx, did: def_id) {
5858
alt cx.ccx.tcx.items.get(did.node) {
5959
ast_map::node_item(item, _) { traverse_public_item(cx, item); }
6060
ast_map::node_method(_, impl_id, _) { traverse_def_id(cx, impl_id); }
61-
ast_map::node_native_item(item, _) { cx.rmap.insert(item.id, ()); }
61+
ast_map::node_native_item(item, _, _) { cx.rmap.insert(item.id, ()); }
6262
ast_map::node_variant(v, _, _) { cx.rmap.insert(v.node.id, ()); }
6363
_ {}
6464
}

src/rustc/middle/ast_map.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import syntax::ast::*;
44
import syntax::ast_util;
55
import syntax::ast_util::inlined_item_methods;
66
import syntax::{visit, codemap};
7+
import driver::session::session;
8+
import front::attr;
79

810
enum path_elt { path_mod(str), path_name(str) }
911
type path = [path_elt];
@@ -24,20 +26,21 @@ fn path_to_str(p: path) -> str {
2426

2527
enum ast_node {
2628
node_item(@item, @path),
27-
node_native_item(@native_item, @path),
29+
node_native_item(@native_item, native_abi, @path),
2830
node_method(@method, def_id /* impl did */, @path /* path to the impl */),
29-
node_variant(variant, def_id, @path),
31+
node_variant(variant, @item, @path),
3032
node_expr(@expr),
3133
node_export(@view_path, @path),
3234
// Locals are numbered, because the alias analysis needs to know in which
3335
// order they are introduced.
3436
node_arg(arg, uint),
3537
node_local(uint),
36-
node_res_ctor(@item),
38+
node_ctor(@item),
3739
}
3840

3941
type map = std::map::hashmap<node_id, ast_node>;
40-
type ctx = {map: map, mutable path: path, mutable local_id: uint};
42+
type ctx = {map: map, mutable path: path,
43+
mutable local_id: uint, sess: session};
4144
type vt = visit::vt<ctx>;
4245

4346
fn extend(cx: ctx, elt: str) -> @path {
@@ -47,7 +50,6 @@ fn extend(cx: ctx, elt: str) -> @path {
4750
fn mk_ast_map_visitor() -> vt {
4851
ret visit::mk_vt(@{
4952
visit_item: map_item,
50-
visit_native_item: map_native_item,
5153
visit_expr: map_expr,
5254
visit_fn: map_fn,
5355
visit_local: map_local,
@@ -57,18 +59,19 @@ fn mk_ast_map_visitor() -> vt {
5759
});
5860
}
5961

60-
fn map_crate(c: crate) -> map {
62+
fn map_crate(sess: session, c: crate) -> map {
6163
let cx = {map: std::map::new_int_hash(),
6264
mutable path: [],
63-
mutable local_id: 0u};
65+
mutable local_id: 0u,
66+
sess: sess};
6467
visit::visit_crate(c, cx, mk_ast_map_visitor());
6568
ret cx.map;
6669
}
6770

6871
// Used for items loaded from external crate that are being inlined into this
6972
// crate. The `path` should be the path to the item but should not include
7073
// the item itself.
71-
fn map_decoded_item(map: map, path: path, ii: inlined_item) {
74+
fn map_decoded_item(sess: session, map: map, path: path, ii: inlined_item) {
7275
// I believe it is ok for the local IDs of inlined items from other crates
7376
// to overlap with the local ids from this crate, so just generate the ids
7477
// starting from 0. (In particular, I think these ids are only used in
@@ -77,7 +80,8 @@ fn map_decoded_item(map: map, path: path, ii: inlined_item) {
7780
// variables that are simultaneously in scope).
7881
let cx = {map: map,
7982
mutable path: path,
80-
mutable local_id: 0u};
83+
mutable local_id: 0u,
84+
sess: sess};
8185
let v = mk_ast_map_visitor();
8286

8387
// methods get added to the AST map when their impl is visited. Since we
@@ -86,7 +90,7 @@ fn map_decoded_item(map: map, path: path, ii: inlined_item) {
8690
alt ii {
8791
ii_item(i) { /* fallthrough */ }
8892
ii_method(impl_did, m) {
89-
map_method(impl_did, @vec::init(path), m, cx);
93+
map_method(impl_did, @path, m, cx);
9094
}
9195
}
9296

@@ -137,19 +141,31 @@ fn map_item(i: @item, cx: ctx, v: vt) {
137141
item_impl(_, _, _, ms) {
138142
let impl_did = ast_util::local_def(i.id);
139143
for m in ms {
140-
map_method(impl_did, item_path, m, cx);
144+
map_method(impl_did, extend(cx, i.ident), m, cx);
141145
}
142146
}
143147
item_res(_, _, _, dtor_id, ctor_id) {
144-
cx.map.insert(ctor_id, node_res_ctor(i));
148+
cx.map.insert(ctor_id, node_ctor(i));
145149
cx.map.insert(dtor_id, node_item(i, item_path));
146150
}
147151
item_enum(vs, _) {
148152
for v in vs {
149153
cx.map.insert(v.node.id, node_variant(
150-
v, ast_util::local_def(i.id), extend(cx, i.ident)));
154+
v, i, extend(cx, i.ident)));
151155
}
152156
}
157+
item_native_mod(nm) {
158+
let abi = alt attr::native_abi(i.attrs) {
159+
either::left(msg) { cx.sess.span_fatal(i.span, msg); }
160+
either::right(abi) { abi }
161+
};
162+
for nitem in nm.items {
163+
cx.map.insert(nitem.id, node_native_item(nitem, abi, @cx.path));
164+
}
165+
}
166+
item_class(_, _, ctor) {
167+
cx.map.insert(ctor.node.id, node_ctor(i));
168+
}
153169
_ { }
154170
}
155171
alt i.node {
@@ -179,11 +195,6 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
179195
}
180196
}
181197

182-
fn map_native_item(i: @native_item, cx: ctx, v: vt) {
183-
cx.map.insert(i.id, node_native_item(i, @cx.path));
184-
visit::visit_native_item(i, cx, v);
185-
}
186-
187198
fn map_expr(ex: @expr, cx: ctx, v: vt) {
188199
cx.map.insert(ex.id, node_expr(ex));
189200
visit::visit_expr(ex, cx, v);

0 commit comments

Comments
 (0)