Skip to content

Commit cc59cea

Browse files
committed
rustc: Thread an item-to-type mapping throughout the typechecking and translation phases
1 parent a154c5b commit cc59cea

File tree

10 files changed

+395
-353
lines changed

10 files changed

+395
-353
lines changed

src/comp/driver/rustc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import front.token;
66
import front.eval;
77
import middle.trans;
88
import middle.resolve;
9+
import middle.ty;
910
import middle.typeck;
1011
import util.common;
1112

@@ -62,8 +63,12 @@ impure fn compile_input(session.session sess,
6263
auto crate = parse_input(sess, p, input);
6364
crate = creader.read_crates(sess, crate, library_search_paths);
6465
crate = resolve.resolve_crate(sess, crate);
65-
crate = typeck.check_crate(sess, crate);
66-
trans.trans_crate(sess, crate, output, shared);
66+
67+
auto typeck_result = typeck.check_crate(sess, crate);
68+
crate = typeck_result._0;
69+
auto type_cache = typeck_result._1;
70+
71+
trans.trans_crate(sess, crate, type_cache, output, shared);
6772
}
6873

6974
impure fn pretty_print_input(session.session sess,

src/comp/front/ast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ tag view_item_ {
395395
view_item_export(ident);
396396
}
397397
398+
type obj_def_ids = rec(def_id ty, def_id ctor);
399+
398400
type item = spanned[item_];
399401
tag item_ {
400402
item_const(ident, @ty, @expr, def_id, ann);
@@ -403,7 +405,7 @@ tag item_ {
403405
item_native_mod(ident, native_mod, def_id);
404406
item_ty(ident, @ty, vec[ty_param], def_id, ann);
405407
item_tag(ident, vec[variant], vec[ty_param], def_id);
406-
item_obj(ident, _obj, vec[ty_param], def_id, ann);
408+
item_obj(ident, _obj, vec[ty_param], obj_def_ids, ann);
407409
}
408410
409411
type native_item = spanned[native_item_];

src/comp/front/creader.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,23 @@ fn read_crates(session.session sess,
544544
}
545545

546546

547+
fn kind_has_type_params(u8 kind_ch) -> bool {
548+
// FIXME: It'd be great if we had u8 char literals.
549+
if (kind_ch == ('c' as u8)) { ret false; }
550+
else if (kind_ch == ('f' as u8)) { ret true; }
551+
else if (kind_ch == ('o' as u8)) { ret true; }
552+
else if (kind_ch == ('t' as u8)) { ret true; }
553+
else if (kind_ch == ('m' as u8)) { ret false; }
554+
else if (kind_ch == ('n' as u8)) { ret false; }
555+
else if (kind_ch == ('v' as u8)) { ret true; }
556+
else {
557+
log #fmt("kind_has_type_params(): unknown kind char: %d",
558+
kind_ch as int);
559+
fail;
560+
}
561+
}
562+
563+
547564
// Crate metadata queries
548565

549566
fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
@@ -567,7 +584,6 @@ fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
567584
auto def;
568585
if (kind_ch == ('c' as u8)) { def = ast.def_const(did); }
569586
else if (kind_ch == ('f' as u8)) { def = ast.def_fn(did); }
570-
else if (kind_ch == ('y' as u8)) { def = ast.def_ty(did); }
571587
else if (kind_ch == ('o' as u8)) { def = ast.def_obj(did); }
572588
else if (kind_ch == ('t' as u8)) { def = ast.def_ty(did); }
573589
else if (kind_ch == ('m' as u8)) { def = ast.def_mod(did); }
@@ -584,13 +600,23 @@ fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
584600
ret some[ast.def](def);
585601
}
586602

587-
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_and_ty {
603+
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_opt_and_ty {
588604
auto external_crate_id = def._0;
589605
auto data = sess.get_external_crate(external_crate_id);
590606
auto ebml_r = lookup_item(def._1, data);
591607
auto t = get_item_type(ebml_r, external_crate_id);
592-
auto tps = get_item_ty_params(ebml_r, external_crate_id);
593-
ret tup(tps, t);
608+
609+
auto tps_opt;
610+
auto kind_ch = get_item_kind(ebml_r);
611+
auto has_ty_params = kind_has_type_params(kind_ch);
612+
if (has_ty_params) {
613+
auto tps = get_item_ty_params(ebml_r, external_crate_id);
614+
tps_opt = some[vec[ast.def_id]](tps);
615+
} else {
616+
tps_opt = none[vec[ast.def_id]];
617+
}
618+
619+
ret tup(tps_opt, t);
594620
}
595621

596622
fn get_symbol(session.session sess, ast.def_id def) -> str {

src/comp/front/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,8 +1876,8 @@ impure fn parse_item_obj(parser p, ast.layer lyr) -> @ast.item {
18761876
methods=meths,
18771877
dtor=dtor);
18781878

1879-
auto item = ast.item_obj(ident, ob, ty_params,
1880-
p.next_def_id(), ast.ann_none);
1879+
auto odid = rec(ty=p.next_def_id(), ctor=p.next_def_id());
1880+
auto item = ast.item_obj(ident, ob, ty_params, odid, ast.ann_none);
18811881

18821882
ret @spanned(lo, hi, item);
18831883
}

src/comp/middle/fold.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ type ast_fold[ENV] =
269269
(fn(&ENV e, &span sp, ident ident,
270270
&ast._obj ob,
271271
vec[ast.ty_param] ty_params,
272-
def_id id, ann a) -> @item) fold_item_obj,
272+
ast.obj_def_ids odid, ann a) -> @item) fold_item_obj,
273273

274274
// View Item folds.
275275
(fn(&ENV e, &span sp, ident ident,
@@ -975,9 +975,9 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
975975
ty_params, id);
976976
}
977977

978-
case (ast.item_obj(?ident, ?ob, ?tps, ?id, ?ann)) {
978+
case (ast.item_obj(?ident, ?ob, ?tps, ?odid, ?ann)) {
979979
let ast._obj ob_ = fold_obj[ENV](env_, fld, ob);
980-
ret fld.fold_item_obj(env_, i.span, ident, ob_, tps, id, ann);
980+
ret fld.fold_item_obj(env_, i.span, ident, ob_, tps, odid, ann);
981981
}
982982

983983
}
@@ -1429,8 +1429,8 @@ fn identity_fold_item_tag[ENV](&ENV e, &span sp, ident i,
14291429

14301430
fn identity_fold_item_obj[ENV](&ENV e, &span sp, ident i,
14311431
&ast._obj ob, vec[ast.ty_param] ty_params,
1432-
def_id id, ann a) -> @item {
1433-
ret @respan(sp, ast.item_obj(i, ob, ty_params, id, a));
1432+
ast.obj_def_ids odid, ann a) -> @item {
1433+
ret @respan(sp, ast.item_obj(i, ob, ty_params, odid, a));
14341434
}
14351435

14361436
// View Item folds.

src/comp/middle/metadata.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const uint tag_items_type = 0x0au;
3030
const uint tag_items_symbol = 0x0bu;
3131
const uint tag_items_variant = 0x0cu;
3232
const uint tag_items_tag_id = 0x0du;
33+
const uint tag_items_obj_type_id = 0x0eu;
3334

3435
// Type encoding
3536

@@ -237,10 +238,11 @@ fn encode_module_item_paths(&ebml.writer ebml_w, &ast._mod module) {
237238
encode_def_id(ebml_w, did);
238239
ebml.end_tag(ebml_w);
239240
}
240-
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
241+
case (ast.item_obj(?id, _, ?tps, ?odid, ?ann)) {
241242
ebml.start_tag(ebml_w, tag_paths_item);
242243
encode_name(ebml_w, id);
243-
encode_def_id(ebml_w, did);
244+
encode_def_id(ebml_w, odid.ctor);
245+
encode_obj_type_id(ebml_w, odid.ty);
244246
ebml.end_tag(ebml_w);
245247
}
246248
}
@@ -301,6 +303,12 @@ fn encode_tag_id(&ebml.writer ebml_w, &ast.def_id id) {
301303
ebml.end_tag(ebml_w);
302304
}
303305

306+
fn encode_obj_type_id(&ebml.writer ebml_w, &ast.def_id id) {
307+
ebml.start_tag(ebml_w, tag_items_obj_type_id);
308+
ebml_w.writer.write(_str.bytes(def_to_str(id)));
309+
ebml.end_tag(ebml_w);
310+
}
311+
304312

305313
fn encode_tag_variant_info(@trans.crate_ctxt cx, &ebml.writer ebml_w,
306314
ast.def_id did, vec[ast.variant] variants) {
@@ -367,13 +375,21 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
367375

368376
encode_tag_variant_info(cx, ebml_w, did, variants);
369377
}
370-
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
378+
case (ast.item_obj(?id, _, ?tps, ?odid, ?ann)) {
371379
ebml.start_tag(ebml_w, tag_items_item);
372-
encode_def_id(ebml_w, did);
380+
encode_def_id(ebml_w, odid.ctor);
373381
encode_kind(ebml_w, 'o' as u8);
374382
encode_type_params(ebml_w, tps);
375-
encode_type(ebml_w, trans.node_ann_type(cx, ann));
376-
encode_symbol(cx, ebml_w, did);
383+
auto fn_ty = trans.node_ann_type(cx, ann);
384+
encode_type(ebml_w, fn_ty);
385+
encode_symbol(cx, ebml_w, odid.ctor);
386+
ebml.end_tag(ebml_w);
387+
388+
ebml.start_tag(ebml_w, tag_items_item);
389+
encode_def_id(ebml_w, odid.ty);
390+
encode_kind(ebml_w, 'y' as u8);
391+
encode_type_params(ebml_w, tps);
392+
encode_type(ebml_w, ty.ty_fn_ret(fn_ty));
377393
ebml.end_tag(ebml_w);
378394
}
379395
}

0 commit comments

Comments
 (0)