@@ -1457,13 +1457,11 @@ fn type_of_variant(@crate_ctxt cx, &ast.variant v) -> TypeRef {
1457
1457
ret T_struct ( lltys) ;
1458
1458
}
1459
1459
1460
- // Returns the number of variants in a tag.
1461
- fn tag_variant_count ( @crate_ctxt cx, ast. def_id id) -> uint {
1460
+ // Returns the variants in a tag.
1461
+ fn tag_variants ( @crate_ctxt cx, ast. def_id id) -> vec [ ast . variant ] {
1462
1462
check ( cx. items. contains_key( id) ) ;
1463
1463
alt ( cx. items. get( id) . node) {
1464
- case ( ast. item_tag( _, ?variants, _, _) ) {
1465
- ret _vec. len[ ast. variant] ( variants) ;
1466
- }
1464
+ case ( ast. item_tag( _, ?variants, _, _) ) { ret variants; }
1467
1465
}
1468
1466
fail; // not reached
1469
1467
}
@@ -1519,21 +1517,9 @@ fn iter_structural_ty(@block_ctxt cx,
1519
1517
case ( ty. ty_tag( ?tid, ?tps) ) {
1520
1518
check ( cx. fcx. ccx. tags. contains_key( tid) ) ;
1521
1519
auto info = cx. fcx. ccx. tags. get( tid) ;
1522
- auto n_variants = tag_variant_count( cx. fcx. ccx, tid) ;
1523
-
1524
- // Look up the tag in the typechecked AST.
1525
- check ( cx. fcx. ccx. items. contains_key( tid) ) ;
1526
- auto tag_item = cx. fcx. ccx. items. get( tid) ;
1527
- let vec[ ast. variant] variants = vec( ) ; // FIXME: typestate bug
1528
- alt ( tag_item. node) {
1529
- case ( ast. item_tag( _, ?vs, _, _) ) {
1530
- variants = vs;
1531
- }
1532
- case ( _) {
1533
- log "trans: ty_tag doesn't actually refer to a tag" ;
1534
- fail;
1535
- }
1536
- }
1520
+
1521
+ auto variants = tag_variants( cx. fcx. ccx, tid) ;
1522
+ auto n_variants = _vec. len[ ast. variant] ( variants) ;
1537
1523
1538
1524
auto lldiscrim_ptr = cx. build. GEP ( v, vec( C_int ( 0 ) , C_int ( 0 ) ) ) ;
1539
1525
auto llunion_ptr = cx. build. GEP ( v, vec( C_int ( 0 ) , C_int ( 1 ) ) ) ;
@@ -1548,55 +1534,49 @@ fn iter_structural_ty(@block_ctxt cx,
1548
1534
auto next_cx = new_sub_block_ctxt( cx, "tag-iter-next" ) ;
1549
1535
1550
1536
auto i = 0 u;
1551
- for ( tup ( ast. def_id , arity ) variant in info . variants) {
1537
+ for ( ast. variant variant in variants) {
1552
1538
auto variant_cx = new_sub_block_ctxt( cx, "tag-iter-variant-" +
1553
1539
_uint. to_str( i, 10 u) ) ;
1554
1540
llvm. LLVMAddCase ( llswitch, C_int ( i as int) , variant_cx. llbb) ;
1555
1541
1556
- alt ( variant. _1) {
1557
- case ( n_ary) {
1558
- let vec[ ValueRef ] vals = vec( C_int ( 0 ) , C_int ( 1 ) ,
1559
- C_int ( i as int) ) ;
1560
- auto llvar = variant_cx. build. GEP ( v, vals) ;
1561
- auto llvarty = type_of_variant( cx. fcx. ccx,
1562
- variants. ( i) ) ;
1563
-
1564
- auto fn_ty = ty. ann_to_type( variants. ( i) . ann) ;
1565
- alt ( fn_ty. struct ) {
1566
- case ( ty. ty_fn( _, ?args, _) ) {
1567
- auto llvarp = variant_cx. build.
1568
- TruncOrBitCast ( llunion_ptr,
1569
- T_ptr ( llvarty) ) ;
1570
-
1571
- auto j = 0 u;
1572
- for ( ty. arg a in args) {
1573
- auto v = vec( C_int ( 0 ) ,
1574
- C_int ( j as int) ) ;
1575
- auto llfldp =
1576
- variant_cx. build. GEP ( llvarp, v) ;
1577
-
1578
- auto ty_subst = ty. substitute_ty_params(
1579
- info. ty_params, tps, a. ty) ;
1580
-
1581
- auto llfld =
1582
- load_scalar_or_boxed( variant_cx,
1583
- llfldp,
1584
- ty_subst) ;
1585
-
1586
- auto res = f( variant_cx, llfld, ty_subst) ;
1587
- variant_cx = res. bcx;
1588
- j += 1 u;
1589
- }
1542
+ if ( _vec. len[ ast. variant_arg] ( variant. args) > 0 u) {
1543
+ // N-ary variant.
1544
+ let vec[ ValueRef ] vals = vec( C_int ( 0 ) , C_int ( 1 ) ,
1545
+ C_int ( i as int) ) ;
1546
+ auto llvar = variant_cx. build. GEP ( v, vals) ;
1547
+ auto llvarty = type_of_variant( cx. fcx. ccx, variants. ( i) ) ;
1548
+
1549
+ auto fn_ty = ty. ann_to_type( variants. ( i) . ann) ;
1550
+ alt ( fn_ty. struct ) {
1551
+ case ( ty. ty_fn( _, ?args, _) ) {
1552
+ auto llvarp = variant_cx. build.
1553
+ TruncOrBitCast ( llunion_ptr, T_ptr ( llvarty) ) ;
1554
+
1555
+ auto j = 0 u;
1556
+ for ( ty. arg a in args) {
1557
+ auto v = vec( C_int ( 0 ) , C_int ( j as int) ) ;
1558
+ auto llfldp = variant_cx. build. GEP ( llvarp, v) ;
1559
+
1560
+ auto ty_subst = ty. substitute_ty_params(
1561
+ info. ty_params, tps, a. ty) ;
1562
+
1563
+ auto llfld =
1564
+ load_scalar_or_boxed( variant_cx,
1565
+ llfldp,
1566
+ ty_subst) ;
1567
+
1568
+ auto res = f( variant_cx, llfld, ty_subst) ;
1569
+ variant_cx = res. bcx;
1570
+ j += 1 u;
1590
1571
}
1591
- case ( _) { fail; }
1592
1572
}
1593
-
1594
- variant_cx. build. Br ( next_cx. llbb) ;
1595
- }
1596
- case ( nullary) {
1597
- // Nothing to do.
1598
- variant_cx. build. Br ( next_cx. llbb) ;
1573
+ case ( _) { fail; }
1599
1574
}
1575
+
1576
+ variant_cx. build. Br ( next_cx. llbb) ;
1577
+ } else {
1578
+ // Nullary variant; nothing to do.
1579
+ variant_cx. build. Br ( next_cx. llbb) ;
1600
1580
}
1601
1581
1602
1582
i += 1 u;
0 commit comments