Skip to content

Commit de0175a

Browse files
committed
rustc: Switch to indices for type parameters
1 parent e11e875 commit de0175a

File tree

9 files changed

+255
-382
lines changed

9 files changed

+255
-382
lines changed

src/comp/front/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type crate_num = int;
1717
type def_num = int;
1818
type def_id = tup(crate_num, def_num);
1919

20-
type ty_param = rec(ident ident, def_id id);
20+
type ty_param = ident;
2121

2222
// Annotations added during successive passes.
2323
tag ann {
@@ -39,7 +39,7 @@ tag def {
3939
def_upvar(def_id);
4040
def_variant(def_id /* tag */, def_id /* variant */);
4141
def_ty(def_id);
42-
def_ty_arg(def_id);
42+
def_ty_arg(uint);
4343
def_binding(def_id);
4444
def_use(def_id);
4545
def_native_ty(def_id);
@@ -59,7 +59,7 @@ fn def_id_of_def(def d) -> def_id {
5959
case (def_upvar(?id)) { ret id; }
6060
case (def_variant(_, ?id)) { ret id; }
6161
case (def_ty(?id)) { ret id; }
62-
case (def_ty_arg(?id)) { ret id; }
62+
case (def_ty_arg(_)) { fail; }
6363
case (def_binding(?id)) { ret id; }
6464
case (def_use(?id)) { ret id; }
6565
case (def_native_ty(?id)) { ret id; }

src/comp/front/creader.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impure fn parse_sty(@pstate st, str_def sd) -> ty.sty {
131131
st.pos = st.pos + 1u;
132132
ret ty.ty_tag(def, params);
133133
}
134-
case ('p') {ret ty.ty_param(parse_def(st, sd));}
134+
case ('p') {ret ty.ty_param(parse_int(st) as uint);}
135135
case ('@') {ret ty.ty_box(parse_mt(st, sd));}
136136
case ('V') {ret ty.ty_vec(parse_mt(st, sd));}
137137
case ('P') {ret ty.ty_port(parse_ty(st, sd));}
@@ -351,14 +351,13 @@ fn item_type(&ebml.doc item, int this_cnum) -> @ty.t {
351351
ret parse_ty_str(s, bind parse_external_def_id(this_cnum, _));
352352
}
353353

354-
fn item_ty_params(&ebml.doc item, int this_cnum) -> vec[ast.def_id] {
355-
let vec[ast.def_id] params = vec();
356-
auto tp = metadata.tag_items_data_item_ty_param;
354+
fn item_ty_param_count(&ebml.doc item, int this_cnum) -> uint {
355+
let uint ty_param_count = 0u;
356+
auto tp = metadata.tag_items_data_item_ty_param_count;
357357
for each (ebml.doc p in ebml.tagged_docs(item, tp)) {
358-
auto ext = parse_def_id(ebml.doc_data(p));
359-
_vec.push[ast.def_id](params, tup(this_cnum, ext._1));
358+
ty_param_count = ebml.vint_at(ebml.doc_data(p), 0u)._0;
360359
}
361-
ret params;
360+
ret ty_param_count;
362361
}
363362

364363
fn tag_variant_ids(&ebml.doc item, int this_cnum) -> vec[ast.def_id] {
@@ -511,23 +510,23 @@ fn lookup_def(session.session sess, int cnum, vec[ast.ident] path)
511510
ret some[ast.def](def);
512511
}
513512

514-
fn get_type(session.session sess, ast.def_id def) -> ty.ty_params_opt_and_ty {
513+
fn get_type(session.session sess, ast.def_id def)
514+
-> ty.ty_param_count_and_ty {
515515
auto external_crate_id = def._0;
516516
auto data = sess.get_external_crate(external_crate_id);
517517
auto item = lookup_item(def._1, data);
518518
auto t = item_type(item, external_crate_id);
519519

520-
auto tps_opt;
520+
auto tp_count;
521521
auto kind_ch = item_kind(item);
522522
auto has_ty_params = kind_has_type_params(kind_ch);
523523
if (has_ty_params) {
524-
auto tps = item_ty_params(item, external_crate_id);
525-
tps_opt = some[vec[ast.def_id]](tps);
524+
tp_count = item_ty_param_count(item, external_crate_id);
526525
} else {
527-
tps_opt = none[vec[ast.def_id]];
526+
tp_count = 0u;
528527
}
529528

530-
ret tup(tps_opt, t);
529+
ret tup(tp_count, t);
531530
}
532531

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

src/comp/front/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,8 +1740,7 @@ impure fn parse_block(parser p) -> ast.block {
17401740
}
17411741

17421742
impure fn parse_ty_param(parser p) -> ast.ty_param {
1743-
auto ident = parse_ident(p);
1744-
ret rec(ident=ident, id=p.next_def_id());
1743+
ret parse_ident(p);
17451744
}
17461745

17471746
impure fn parse_ty_params(parser p) -> vec[ast.ty_param] {

src/comp/middle/metadata.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const uint tag_def_id = 0x07u;
2929
const uint tag_items_data = 0x08u;
3030
const uint tag_items_data_item = 0x09u;
3131
const uint tag_items_data_item_kind = 0x0au;
32-
const uint tag_items_data_item_ty_param = 0x0bu;
32+
const uint tag_items_data_item_ty_param_count = 0x0bu;
3333
const uint tag_items_data_item_type = 0x0cu;
3434
const uint tag_items_data_item_symbol = 0x0du;
3535
const uint tag_items_data_item_variant = 0x0eu;
@@ -134,7 +134,7 @@ fn sty_str(ty.sty st, def_str ds) -> str {
134134
}
135135
case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
136136
case (ty.ty_native) {ret "E";}
137-
case (ty.ty_param(?def)) {ret "p" + ds(def) + "|";}
137+
case (ty.ty_param(?id)) {ret "p" + common.uistr(id);}
138138
case (ty.ty_type) {ret "Y";}
139139
}
140140
}
@@ -310,13 +310,10 @@ fn def_to_str(ast.def_id did) -> str {
310310
ret #fmt("%d:%d", did._0, did._1);
311311
}
312312

313-
// TODO: We need to encode the "crate numbers" somewhere for diamond imports.
314-
fn encode_type_params(&ebml.writer ebml_w, vec[ast.ty_param] tps) {
315-
for (ast.ty_param tp in tps) {
316-
ebml.start_tag(ebml_w, tag_items_data_item_ty_param);
317-
ebml_w.writer.write(_str.bytes(def_to_str(tp.id)));
318-
ebml.end_tag(ebml_w);
319-
}
313+
fn encode_type_param_count(&ebml.writer ebml_w, vec[ast.ty_param] tps) {
314+
ebml.start_tag(ebml_w, tag_items_data_item_ty_param_count);
315+
ebml.write_vint(ebml_w.writer, _vec.len[ast.ty_param](tps));
316+
ebml.end_tag(ebml_w);
320317
}
321318

322319
fn encode_variant_id(&ebml.writer ebml_w, ast.def_id vid) {
@@ -374,7 +371,7 @@ fn encode_tag_variant_info(@trans.crate_ctxt cx, &ebml.writer ebml_w,
374371
encode_symbol(cx, ebml_w, variant.node.id);
375372
}
376373
encode_discriminant(cx, ebml_w, variant.node.id);
377-
encode_type_params(ebml_w, ty_params);
374+
encode_type_param_count(ebml_w, ty_params);
378375
ebml.end_tag(ebml_w);
379376
}
380377
}
@@ -394,7 +391,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
394391
ebml.start_tag(ebml_w, tag_items_data_item);
395392
encode_def_id(ebml_w, did);
396393
encode_kind(ebml_w, 'f' as u8);
397-
encode_type_params(ebml_w, tps);
394+
encode_type_param_count(ebml_w, tps);
398395
encode_type(ebml_w, trans.node_ann_type(cx, ann));
399396
encode_symbol(cx, ebml_w, did);
400397
ebml.end_tag(ebml_w);
@@ -415,15 +412,15 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
415412
ebml.start_tag(ebml_w, tag_items_data_item);
416413
encode_def_id(ebml_w, did);
417414
encode_kind(ebml_w, 'y' as u8);
418-
encode_type_params(ebml_w, tps);
415+
encode_type_param_count(ebml_w, tps);
419416
encode_type(ebml_w, trans.node_ann_type(cx, ann));
420417
ebml.end_tag(ebml_w);
421418
}
422419
case (ast.item_tag(?id, ?variants, ?tps, ?did, ?ann)) {
423420
ebml.start_tag(ebml_w, tag_items_data_item);
424421
encode_def_id(ebml_w, did);
425422
encode_kind(ebml_w, 't' as u8);
426-
encode_type_params(ebml_w, tps);
423+
encode_type_param_count(ebml_w, tps);
427424
encode_type(ebml_w, trans.node_ann_type(cx, ann));
428425
for (ast.variant v in variants) {
429426
encode_variant_id(ebml_w, v.node.id);
@@ -436,7 +433,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
436433
ebml.start_tag(ebml_w, tag_items_data_item);
437434
encode_def_id(ebml_w, odid.ctor);
438435
encode_kind(ebml_w, 'o' as u8);
439-
encode_type_params(ebml_w, tps);
436+
encode_type_param_count(ebml_w, tps);
440437
auto fn_ty = trans.node_ann_type(cx, ann);
441438
encode_type(ebml_w, fn_ty);
442439
encode_symbol(cx, ebml_w, odid.ctor);
@@ -445,7 +442,7 @@ fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
445442
ebml.start_tag(ebml_w, tag_items_data_item);
446443
encode_def_id(ebml_w, odid.ty);
447444
encode_kind(ebml_w, 'y' as u8);
448-
encode_type_params(ebml_w, tps);
445+
encode_type_param_count(ebml_w, tps);
449446
encode_type(ebml_w, ty.ty_fn_ret(fn_ty));
450447
ebml.end_tag(ebml_w);
451448
}
@@ -464,7 +461,7 @@ fn encode_info_for_native_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
464461
case (ast.native_item_fn(_, _, _, ?tps, ?did, ?ann)) {
465462
encode_def_id(ebml_w, did);
466463
encode_kind(ebml_w, 'F' as u8);
467-
encode_type_params(ebml_w, tps);
464+
encode_type_param_count(ebml_w, tps);
468465
encode_type(ebml_w, trans.node_ann_type(cx, ann));
469466
encode_symbol(cx, ebml_w, did);
470467
}

src/comp/middle/resolve.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,22 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
398398
}
399399
}
400400

401-
fn handle_fn_decl(ast.ident i, &ast.fn_decl decl,
401+
fn handle_fn_decl(ast.ident identifier, &ast.fn_decl decl,
402402
&vec[ast.ty_param] ty_params) -> option.t[def_wrap] {
403403
for (ast.arg a in decl.inputs) {
404-
if (_str.eq(a.ident, i)) {
404+
if (_str.eq(a.ident, identifier)) {
405405
auto t = ast.def_arg(a.id);
406406
ret some(def_wrap_other(t));
407407
}
408408
}
409+
410+
auto i = 0u;
409411
for (ast.ty_param tp in ty_params) {
410-
if (_str.eq(tp.ident, i)) {
411-
auto t = ast.def_ty_arg(tp.id);
412+
if (_str.eq(tp, identifier)) {
413+
auto t = ast.def_ty_arg(i);
412414
ret some(def_wrap_other(t));
413415
}
416+
i += 1u;
414417
}
415418
ret none[def_wrap];
416419
}
@@ -450,53 +453,60 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
450453
}
451454
}
452455

453-
fn in_scope(&session.session sess, ast.ident i, &scope s, namespace ns)
454-
-> option.t[def_wrap] {
456+
fn in_scope(&session.session sess, ast.ident identifier, &scope s,
457+
namespace ns) -> option.t[def_wrap] {
455458
alt (s) {
456459

457460
case (scope_crate(?c)) {
458-
ret check_mod(i, c.node.module, ns);
461+
ret check_mod(identifier, c.node.module, ns);
459462
}
460463

461464
case (scope_item(?it)) {
462465
alt (it.node) {
463466
case (ast.item_fn(_, ?f, ?ty_params, _, _)) {
464-
ret handle_fn_decl(i, f.decl, ty_params);
467+
ret handle_fn_decl(identifier, f.decl, ty_params);
465468
}
466469
case (ast.item_obj(_, ?ob, ?ty_params, _, _)) {
467470
for (ast.obj_field f in ob.fields) {
468-
if (_str.eq(f.ident, i)) {
471+
if (_str.eq(f.ident, identifier)) {
469472
auto t = ast.def_obj_field(f.id);
470473
ret some(def_wrap_other(t));
471474
}
472475
}
476+
477+
auto i = 0u;
473478
for (ast.ty_param tp in ty_params) {
474-
if (_str.eq(tp.ident, i)) {
475-
auto t = ast.def_ty_arg(tp.id);
479+
if (_str.eq(tp, identifier)) {
480+
auto t = ast.def_ty_arg(i);
476481
ret some(def_wrap_other(t));
477482
}
483+
i += 1u;
478484
}
479485
}
480486
case (ast.item_tag(_,?variants,?ty_params,?tag_id,_)) {
487+
auto i = 0u;
481488
for (ast.ty_param tp in ty_params) {
482-
if (_str.eq(tp.ident, i)) {
483-
auto t = ast.def_ty_arg(tp.id);
489+
if (_str.eq(tp, identifier)) {
490+
auto t = ast.def_ty_arg(i);
484491
ret some(def_wrap_other(t));
485492
}
493+
i += 1u;
486494
}
487495
}
488496
case (ast.item_mod(_, ?m, _)) {
489-
ret check_mod(i, m, ns);
497+
ret check_mod(identifier, m, ns);
490498
}
491499
case (ast.item_native_mod(_, ?m, _)) {
492-
ret check_native_mod(i, m);
500+
ret check_native_mod(identifier, m);
493501
}
494502
case (ast.item_ty(_, _, ?ty_params, _, _)) {
503+
auto i = 0u;
495504
for (ast.ty_param tp in ty_params) {
496-
if (_str.eq(tp.ident, i)) {
497-
auto t = ast.def_ty_arg(tp.id);
505+
if (_str.eq(tp, identifier)) {
506+
auto t = ast.def_ty_arg(i);
498507
ret some(def_wrap_other(t));
499508
}
509+
i += 1u;
500510
}
501511
}
502512
case (_) { /* fall through */ }
@@ -506,7 +516,7 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
506516
case (scope_native_item(?it)) {
507517
alt (it.node) {
508518
case (ast.native_item_fn(_, _, ?decl, ?ty_params, _, _)) {
509-
ret handle_fn_decl(i, decl, ty_params);
519+
ret handle_fn_decl(identifier, decl, ty_params);
510520
}
511521
}
512522
}
@@ -518,7 +528,7 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
518528
case (scope_loop(?d)) {
519529
alt (d.node) {
520530
case (ast.decl_local(?local)) {
521-
if (_str.eq(local.ident, i)) {
531+
if (_str.eq(local.ident, identifier)) {
522532
auto lc = ast.def_local(local.id);
523533
ret some(def_wrap_other(lc));
524534
}
@@ -527,11 +537,11 @@ fn lookup_name_wrapped(&env e, ast.ident i, namespace ns)
527537
}
528538

529539
case (scope_block(?b)) {
530-
ret check_block(i, b.node, ns);
540+
ret check_block(identifier, b.node, ns);
531541
}
532542

533543
case (scope_arm(?a)) {
534-
alt (a.index.find(i)) {
544+
alt (a.index.find(identifier)) {
535545
case (some[ast.def_id](?did)) {
536546
auto t = ast.def_binding(did);
537547
ret some[def_wrap](def_wrap_other(t));

0 commit comments

Comments
 (0)