Skip to content

Commit 15f71b3

Browse files
committed
Refactor ast::item representation
Most of the fields in an AST item were present in all variants. Things could be simplified considerably by putting them in the rec rather than in the variant tags.
1 parent 6c2f322 commit 15f71b3

File tree

15 files changed

+311
-382
lines changed

15 files changed

+311
-382
lines changed

src/comp/front/ast.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -459,28 +459,21 @@ tag attr_style { attr_outer; attr_inner; }
459459

460460
type attribute_ = rec(attr_style style, meta_item value);
461461

462-
type item = spanned[item_];
462+
type item = rec(ident ident,
463+
vec[attribute] attrs,
464+
def_id id, // For objs, this is the type def_id
465+
ann ann,
466+
item_ node,
467+
span span);
463468

464469
tag item_ {
465-
item_const(ident, @ty, @expr, vec[attribute], def_id, ann);
466-
item_fn(ident, _fn, vec[ty_param], vec[attribute], def_id, ann);
467-
item_mod(ident, _mod, vec[attribute], def_id);
468-
item_native_mod(ident, native_mod, vec[attribute], def_id);
469-
item_ty(ident, @ty, vec[ty_param], vec[attribute], def_id, ann);
470-
item_tag(ident, vec[variant], vec[ty_param], vec[attribute], def_id, ann);
471-
item_obj(ident, _obj, vec[ty_param], vec[attribute], obj_def_ids, ann);
472-
}
473-
474-
fn item_ident(@item it) -> ident {
475-
ret alt (it.node) {
476-
case (item_const(?ident, _, _, _, _, _)) { ident }
477-
case (item_fn(?ident, _, _, _, _, _)) { ident }
478-
case (item_mod(?ident, _, _, _)) { ident }
479-
case (item_native_mod(?ident, _, _, _)) { ident }
480-
case (item_ty(?ident, _, _, _, _, _)) { ident }
481-
case (item_tag(?ident, _, _, _, _, _)) { ident }
482-
case (item_obj(?ident, _, _, _, _, _)) { ident }
483-
}
470+
item_const(@ty, @expr);
471+
item_fn(_fn, vec[ty_param]);
472+
item_mod(_mod);
473+
item_native_mod(native_mod);
474+
item_ty(@ty, vec[ty_param]);
475+
item_tag(vec[variant], vec[ty_param]);
476+
item_obj(_obj, vec[ty_param], def_id /* constructor id */);
484477
}
485478

486479
type native_item = spanned[native_item_];
@@ -498,15 +491,16 @@ tag native_item_ {
498491
fn is_exported(ident i, _mod m) -> bool {
499492
auto nonlocal = true;
500493
for (@ast::item it in m.items) {
501-
if (item_ident(it) == i) { nonlocal = false; }
494+
if (it.ident == i) { nonlocal = false; }
502495
alt (it.node) {
503-
case (item_tag(_, ?variants, _, _, _, _)) {
496+
case (item_tag(?variants, _)) {
504497
for (variant v in variants) {
505498
if (v.node.name == i) { nonlocal = false; }
506499
}
507500
}
508501
case (_) { }
509502
}
503+
if (!nonlocal) { break; }
510504
}
511505
auto count = 0u;
512506
for (@ast::view_item vi in m.view_items) {
@@ -525,7 +519,7 @@ fn is_exported(ident i, _mod m) -> bool {
525519

526520
// If there are no declared exports then
527521
// everything not imported is exported
528-
if (count == 0u && !nonlocal) { ret true; } else { ret false; }
522+
ret count == 0u && !nonlocal;
529523
}
530524

531525
fn is_call_expr(@expr e) -> bool {

src/comp/front/eval.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,17 @@ fn eval_crate_directive(ctx cx, env e, @ast::crate_directive cdir, str prefix,
294294
cx.p.set_def(next_id._1);
295295
cx.chpos = p0.get_chpos();
296296
cx.next_ann = p0.next_ann_num();
297-
auto im = ast::item_mod(id, m0, [], next_id);
298-
auto i = @spanned(cdir.span.lo, cdir.span.hi, im);
297+
auto i = front::parser::mk_item(cx.p, cdir.span.lo, cdir.span.hi,
298+
id, ast::item_mod(m0), []);
299299
vec::push[@ast::item](items, i);
300300
}
301301
case (ast::cdir_dir_mod(?id, ?dir_opt, ?cdirs)) {
302302
auto path = id;
303303
alt (dir_opt) { case (some(?d)) { path = d; } case (none) { } }
304304
auto full_path = prefix + std::fs::path_sep() + path;
305305
auto m0 = eval_crate_directives_to_mod(cx, e, cdirs, full_path);
306-
auto im = ast::item_mod(id, m0, [], cx.p.next_def_id());
307-
auto i = @spanned(cdir.span.lo, cdir.span.hi, im);
306+
auto i = front::parser::mk_item
307+
(cx.p, cdir.span.lo, cdir.span.hi, id, ast::item_mod(m0), []);
308308
vec::push[@ast::item](items, i);
309309
}
310310
case (ast::cdir_view_item(?vi)) {

src/comp/front/parser.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,14 +1662,22 @@ fn parse_fn_header(&parser p) -> tup(ast::ident, vec[ast::ty_param]) {
16621662
ret tup(id, ty_params);
16631663
}
16641664

1665+
fn mk_item(&parser p, uint lo, uint hi, &ast::ident ident, &ast::item_ node,
1666+
&vec[ast::attribute] attrs) -> @ast::item {
1667+
ret @rec(ident=ident,
1668+
attrs=attrs,
1669+
id=p.next_def_id(),
1670+
ann=p.get_ann(),
1671+
node=node,
1672+
span=rec(lo=lo, hi=hi));
1673+
}
1674+
16651675
fn parse_item_fn_or_iter(&parser p, ast::purity purity, ast::proto proto,
16661676
vec[ast::attribute] attrs) -> @ast::item {
16671677
auto lo = p.get_last_lo_pos();
16681678
auto t = parse_fn_header(p);
16691679
auto f = parse_fn(p, proto, purity);
1670-
auto item =
1671-
ast::item_fn(t._0, f, t._1, attrs, p.next_def_id(), p.get_ann());
1672-
ret @spanned(lo, f.body.span.hi, item);
1680+
ret mk_item(p, lo, f.body.span.hi, t._0, ast::item_fn(f, t._1), attrs);
16731681
}
16741682

16751683
fn parse_obj_field(&parser p) -> ast::obj_field {
@@ -1727,9 +1735,8 @@ fn parse_item_obj(&parser p, ast::layer lyr, vec[ast::attribute] attrs) ->
17271735
auto hi = p.get_hi_pos();
17281736
expect(p, token::RBRACE);
17291737
let ast::_obj ob = rec(fields=fields.node, methods=meths, dtor=dtor);
1730-
auto odid = rec(ty=p.next_def_id(), ctor=p.next_def_id());
1731-
auto item = ast::item_obj(ident, ob, ty_params, attrs, odid, p.get_ann());
1732-
ret @spanned(lo, hi, item);
1738+
ret mk_item(p, lo, hi, ident, ast::item_obj(ob, ty_params,
1739+
p.next_def_id()), attrs);
17331740
}
17341741

17351742
fn parse_mod_items(&parser p, token::token term) -> ast::_mod {
@@ -1756,9 +1763,7 @@ fn parse_item_const(&parser p, vec[ast::attribute] attrs) -> @ast::item {
17561763
auto e = parse_expr(p);
17571764
auto hi = p.get_hi_pos();
17581765
expect(p, token::SEMI);
1759-
auto item =
1760-
ast::item_const(id, ty, e, attrs, p.next_def_id(), p.get_ann());
1761-
ret @spanned(lo, hi, item);
1766+
ret mk_item(p, lo, hi, id, ast::item_const(ty, e), attrs);
17621767
}
17631768

17641769
fn parse_item_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
@@ -1768,8 +1773,7 @@ fn parse_item_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
17681773
auto m = parse_mod_items(p, token::RBRACE);
17691774
auto hi = p.get_hi_pos();
17701775
expect(p, token::RBRACE);
1771-
auto item = ast::item_mod(id, m, attrs, p.next_def_id());
1772-
ret @spanned(lo, hi, item);
1776+
ret mk_item(p, lo, hi, id, ast::item_mod(m), attrs);
17731777
}
17741778

17751779
fn parse_item_native_type(&parser p) -> @ast::native_item {
@@ -1856,8 +1860,7 @@ fn parse_item_native_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
18561860
auto m = parse_native_mod_items(p, native_name, abi);
18571861
auto hi = p.get_hi_pos();
18581862
expect(p, token::RBRACE);
1859-
auto item = ast::item_native_mod(id, m, attrs, p.next_def_id());
1860-
ret @spanned(lo, hi, item);
1863+
ret mk_item(p, lo, hi, id, ast::item_native_mod(m), attrs);
18611864
}
18621865

18631866
fn parse_type_decl(&parser p) -> tup(uint, ast::ident) {
@@ -1873,9 +1876,7 @@ fn parse_item_type(&parser p, vec[ast::attribute] attrs) -> @ast::item {
18731876
auto ty = parse_ty(p);
18741877
auto hi = p.get_hi_pos();
18751878
expect(p, token::SEMI);
1876-
auto item =
1877-
ast::item_ty(t._1, ty, tps, attrs, p.next_def_id(), p.get_ann());
1878-
ret @spanned(t._0, hi, item);
1879+
ret mk_item(p, t._0, hi, t._1, ast::item_ty(ty, tps), attrs);
18791880
}
18801881

18811882
fn parse_item_tag(&parser p, vec[ast::attribute] attrs) -> @ast::item {
@@ -1922,10 +1923,7 @@ fn parse_item_tag(&parser p, vec[ast::attribute] attrs) -> @ast::item {
19221923
}
19231924
auto hi = p.get_hi_pos();
19241925
p.bump();
1925-
auto item =
1926-
ast::item_tag(id, variants, ty_params, attrs, p.next_def_id(),
1927-
p.get_ann());
1928-
ret @spanned(lo, hi, item);
1926+
ret mk_item(p, lo, hi, id, ast::item_tag(variants, ty_params), attrs);
19291927
}
19301928

19311929
fn parse_layer(&parser p) -> ast::layer {

src/comp/middle/alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn visit_fn(@ctx cx, &ast::_fn f, &vec[ast::ty_param] tp, &span sp,
6363

6464
fn visit_item(@ctx cx, &@ast::item i, &scope sc, &vt[scope] v) {
6565
alt (i.node) {
66-
case (ast::item_obj(_, ?o, _, _, _, _)) {
66+
case (ast::item_obj(?o, _, _)) {
6767
for (ast::obj_field f in o.fields) {
6868
cx.local_map.insert(f.id._1, objfield(f.mut));
6969
}

src/comp/middle/metadata.rs

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -361,64 +361,65 @@ fn encode_module_item_paths(&ebml::writer ebml_w, &ast::_mod module,
361361
&vec[str] path,
362362
&mutable vec[tup(str, uint)] index) {
363363
for (@ast::item it in module.items) {
364-
if (!ast::is_exported(ast::item_ident(it), module)) { cont; }
364+
if (!ast::is_exported(it.ident, module)) { cont; }
365365
alt (it.node) {
366-
case (ast::item_const(?id, _, ?tps, _, ?did, ?ann)) {
367-
add_to_index(ebml_w, path, index, id);
366+
case (ast::item_const(_, _)) {
367+
add_to_index(ebml_w, path, index, it.ident);
368368
ebml::start_tag(ebml_w, tag_paths_data_item);
369-
encode_name(ebml_w, id);
370-
encode_def_id(ebml_w, did);
369+
encode_name(ebml_w, it.ident);
370+
encode_def_id(ebml_w, it.id);
371371
ebml::end_tag(ebml_w);
372372
}
373-
case (ast::item_fn(?id, _, ?tps, _, ?did, ?ann)) {
374-
add_to_index(ebml_w, path, index, id);
373+
case (ast::item_fn(_, ?tps)) {
374+
add_to_index(ebml_w, path, index, it.ident);
375375
ebml::start_tag(ebml_w, tag_paths_data_item);
376-
encode_name(ebml_w, id);
377-
encode_def_id(ebml_w, did);
376+
encode_name(ebml_w, it.ident);
377+
encode_def_id(ebml_w, it.id);
378378
ebml::end_tag(ebml_w);
379379
}
380-
case (ast::item_mod(?id, ?_mod, _, ?did)) {
381-
add_to_index(ebml_w, path, index, id);
380+
case (ast::item_mod(?_mod)) {
381+
add_to_index(ebml_w, path, index, it.ident);
382382
ebml::start_tag(ebml_w, tag_paths_data_mod);
383-
encode_name(ebml_w, id);
384-
encode_def_id(ebml_w, did);
385-
encode_module_item_paths(ebml_w, _mod, path + [id], index);
383+
encode_name(ebml_w, it.ident);
384+
encode_def_id(ebml_w, it.id);
385+
encode_module_item_paths(ebml_w, _mod, path + [it.ident],
386+
index);
386387
ebml::end_tag(ebml_w);
387388
}
388-
case (ast::item_native_mod(?id, ?nmod, _, ?did)) {
389-
add_to_index(ebml_w, path, index, id);
389+
case (ast::item_native_mod(?nmod)) {
390+
add_to_index(ebml_w, path, index, it.ident);
390391
ebml::start_tag(ebml_w, tag_paths_data_mod);
391-
encode_name(ebml_w, id);
392-
encode_def_id(ebml_w, did);
393-
encode_native_module_item_paths(ebml_w, nmod, path + [id],
394-
index);
392+
encode_name(ebml_w, it.ident);
393+
encode_def_id(ebml_w, it.id);
394+
encode_native_module_item_paths(ebml_w, nmod,
395+
path + [it.ident], index);
395396
ebml::end_tag(ebml_w);
396397
}
397-
case (ast::item_ty(?id, _, ?tps, _, ?did, ?ann)) {
398-
add_to_index(ebml_w, path, index, id);
398+
case (ast::item_ty(_, ?tps)) {
399+
add_to_index(ebml_w, path, index, it.ident);
399400
ebml::start_tag(ebml_w, tag_paths_data_item);
400-
encode_name(ebml_w, id);
401-
encode_def_id(ebml_w, did);
401+
encode_name(ebml_w, it.ident);
402+
encode_def_id(ebml_w, it.id);
402403
ebml::end_tag(ebml_w);
403404
}
404-
case (ast::item_tag(?id, ?variants, ?tps, _, ?did, _)) {
405-
add_to_index(ebml_w, path, index, id);
405+
case (ast::item_tag(?variants, ?tps)) {
406+
add_to_index(ebml_w, path, index, it.ident);
406407
ebml::start_tag(ebml_w, tag_paths_data_item);
407-
encode_name(ebml_w, id);
408-
encode_def_id(ebml_w, did);
408+
encode_name(ebml_w, it.ident);
409+
encode_def_id(ebml_w, it.id);
409410
ebml::end_tag(ebml_w);
410411
encode_tag_variant_paths(ebml_w, variants, path, index);
411412
}
412-
case (ast::item_obj(?id, _, ?tps, _, ?odid, ?ann)) {
413-
add_to_index(ebml_w, path, index, id);
413+
case (ast::item_obj(_, ?tps, ?ctor_id)) {
414+
add_to_index(ebml_w, path, index, it.ident);
414415
ebml::start_tag(ebml_w, tag_paths_data_item);
415-
encode_name(ebml_w, id);
416-
encode_def_id(ebml_w, odid.ctor);
416+
encode_name(ebml_w, it.ident);
417+
encode_def_id(ebml_w, ctor_id);
417418
ebml::end_tag(ebml_w);
418-
add_to_index(ebml_w, path, index, id);
419+
add_to_index(ebml_w, path, index, it.ident);
419420
ebml::start_tag(ebml_w, tag_paths_data_item);
420-
encode_name(ebml_w, id);
421-
encode_def_id(ebml_w, odid.ty);
421+
encode_name(ebml_w, it.ident);
422+
encode_def_id(ebml_w, it.id);
422423
ebml::end_tag(ebml_w);
423424
}
424425
}
@@ -509,67 +510,68 @@ fn encode_tag_variant_info(&@trans::crate_ctxt cx, &ebml::writer ebml_w,
509510
fn encode_info_for_item(@trans::crate_ctxt cx, &ebml::writer ebml_w,
510511
@ast::item item, &mutable vec[tup(int, uint)] index) {
511512
alt (item.node) {
512-
case (ast::item_const(_, _, _, _, ?did, ?ann)) {
513+
case (ast::item_const(_, _)) {
513514
ebml::start_tag(ebml_w, tag_items_data_item);
514-
encode_def_id(ebml_w, did);
515+
encode_def_id(ebml_w, item.id);
515516
encode_kind(ebml_w, 'c' as u8);
516-
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
517-
encode_symbol(cx, ebml_w, did);
517+
encode_type(cx, ebml_w, trans::node_ann_type(cx, item.ann));
518+
encode_symbol(cx, ebml_w, item.id);
518519
ebml::end_tag(ebml_w);
519520
}
520-
case (ast::item_fn(_, _, ?tps, _, ?did, ?ann)) {
521+
case (ast::item_fn(_, ?tps)) {
521522
ebml::start_tag(ebml_w, tag_items_data_item);
522-
encode_def_id(ebml_w, did);
523+
encode_def_id(ebml_w, item.id);
523524
encode_kind(ebml_w, 'f' as u8);
524525
encode_type_param_count(ebml_w, tps);
525-
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
526-
encode_symbol(cx, ebml_w, did);
526+
encode_type(cx, ebml_w, trans::node_ann_type(cx, item.ann));
527+
encode_symbol(cx, ebml_w, item.id);
527528
ebml::end_tag(ebml_w);
528529
}
529-
case (ast::item_mod(_, _, _, ?did)) {
530+
case (ast::item_mod(_)) {
530531
ebml::start_tag(ebml_w, tag_items_data_item);
531-
encode_def_id(ebml_w, did);
532+
encode_def_id(ebml_w, item.id);
532533
encode_kind(ebml_w, 'm' as u8);
533534
ebml::end_tag(ebml_w);
534535
}
535-
case (ast::item_native_mod(?id, _, _, ?did)) {
536+
case (ast::item_native_mod(_)) {
536537
ebml::start_tag(ebml_w, tag_items_data_item);
537-
encode_def_id(ebml_w, did);
538+
encode_def_id(ebml_w, item.id);
538539
encode_kind(ebml_w, 'n' as u8);
539540
ebml::end_tag(ebml_w);
540541
}
541-
case (ast::item_ty(?id, _, ?tps, _, ?did, ?ann)) {
542+
case (ast::item_ty(_, ?tps)) {
542543
ebml::start_tag(ebml_w, tag_items_data_item);
543-
encode_def_id(ebml_w, did);
544+
encode_def_id(ebml_w, item.id);
544545
encode_kind(ebml_w, 'y' as u8);
545546
encode_type_param_count(ebml_w, tps);
546-
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
547+
encode_type(cx, ebml_w, trans::node_ann_type(cx, item.ann));
547548
ebml::end_tag(ebml_w);
548549
}
549-
case (ast::item_tag(?id, ?variants, ?tps, _, ?did, ?ann)) {
550+
case (ast::item_tag(?variants, ?tps)) {
550551
ebml::start_tag(ebml_w, tag_items_data_item);
551-
encode_def_id(ebml_w, did);
552+
encode_def_id(ebml_w, item.id);
552553
encode_kind(ebml_w, 't' as u8);
553554
encode_type_param_count(ebml_w, tps);
554-
encode_type(cx, ebml_w, trans::node_ann_type(cx, ann));
555+
encode_type(cx, ebml_w, trans::node_ann_type(cx, item.ann));
555556
for (ast::variant v in variants) {
556557
encode_variant_id(ebml_w, v.node.id);
557558
}
558559
ebml::end_tag(ebml_w);
559-
encode_tag_variant_info(cx, ebml_w, did, variants, index, tps);
560+
encode_tag_variant_info(cx, ebml_w, item.id, variants,
561+
index, tps);
560562
}
561-
case (ast::item_obj(?id, _, ?tps, _, ?odid, ?ann)) {
563+
case (ast::item_obj(_, ?tps, ?ctor_id)) {
562564
ebml::start_tag(ebml_w, tag_items_data_item);
563-
encode_def_id(ebml_w, odid.ctor);
565+
encode_def_id(ebml_w, ctor_id);
564566
encode_kind(ebml_w, 'o' as u8);
565567
encode_type_param_count(ebml_w, tps);
566-
auto fn_ty = trans::node_ann_type(cx, ann);
568+
auto fn_ty = trans::node_ann_type(cx, item.ann);
567569
encode_type(cx, ebml_w, fn_ty);
568-
encode_symbol(cx, ebml_w, odid.ctor);
570+
encode_symbol(cx, ebml_w, ctor_id);
569571
ebml::end_tag(ebml_w);
570-
index += [tup(odid.ty._1, ebml_w.writer.tell())];
572+
index += [tup(item.id._1, ebml_w.writer.tell())];
571573
ebml::start_tag(ebml_w, tag_items_data_item);
572-
encode_def_id(ebml_w, odid.ty);
574+
encode_def_id(ebml_w, item.id);
573575
encode_kind(ebml_w, 'y' as u8);
574576
encode_type_param_count(ebml_w, tps);
575577
encode_type(cx, ebml_w, ty::ty_fn_ret(cx.tcx, fn_ty));

0 commit comments

Comments
 (0)