Skip to content

Commit dcb9d9b

Browse files
committed
[constfold.d] remove use of gotos
1 parent 919e810 commit dcb9d9b

File tree

1 file changed

+40
-59
lines changed

1 file changed

+40
-59
lines changed

compiler/src/dmd/constfold.d

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,6 @@ UnionExp Equal(EXP op, Loc loc, Type type, Expression e1, Expression e2)
611611
{
612612
UnionExp ue = void;
613613
int cmp = 0;
614-
real_t r1 = CTFloat.zero;
615-
real_t r2 = CTFloat.zero;
616614
//printf("Equal(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
617615
assert(op == EXP.equal || op == EXP.notEqual);
618616
if (e1.op == EXP.null_)
@@ -691,17 +689,17 @@ UnionExp Equal(EXP op, Loc loc, Type type, Expression e1, Expression e2)
691689
}
692690
}
693691
}
694-
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.string_)
692+
else if ((e1.op == EXP.arrayLiteral && e2.op == EXP.string_) ||
693+
(e1.op == EXP.string_ && e2.op == EXP.arrayLiteral))
695694
{
696-
// Swap operands and use common code
697-
Expression etmp = e1;
698-
e1 = e2;
699-
e2 = etmp;
700-
goto Lsa;
701-
}
702-
else if (e1.op == EXP.string_ && e2.op == EXP.arrayLiteral)
703-
{
704-
Lsa:
695+
if (e1.op == EXP.arrayLiteral)
696+
{
697+
// Swap operands and use common code
698+
Expression etmp = e1;
699+
e1 = e2;
700+
e2 = etmp;
701+
}
702+
705703
StringExp es1 = e1.isStringExp();
706704
ArrayLiteralExp es2 = e2.isArrayLiteralExp();
707705
size_t dim1 = es1.len;
@@ -766,17 +764,12 @@ UnionExp Equal(EXP op, Loc loc, Type type, Expression e1, Expression e2)
766764
cantExp(ue);
767765
return ue;
768766
}
769-
else if (e1.type.isReal())
767+
else if (e1.type.isReal() || e1.type.isImaginary())
770768
{
771-
r1 = e1.toReal();
772-
r2 = e2.toReal();
773-
goto L1;
774-
}
775-
else if (e1.type.isImaginary())
776-
{
777-
r1 = e1.toImaginary();
778-
r2 = e2.toImaginary();
779-
L1:
769+
const bool isReal = e1.type.isReal();
770+
real_t r1 = isReal ? e1.toReal() : e1.toImaginary();
771+
real_t r2 = isReal ? e2.toReal() : e2.toImaginary();
772+
780773
if (CTFloat.isNaN(r1) || CTFloat.isNaN(r2)) // if unordered
781774
{
782775
cmp = 0;
@@ -854,8 +847,6 @@ UnionExp Cmp(EXP op, Loc loc, Type type, Expression e1, Expression e2)
854847
{
855848
UnionExp ue = void;
856849
dinteger_t n;
857-
real_t r1 = CTFloat.zero;
858-
real_t r2 = CTFloat.zero;
859850
//printf("Cmp(e1 = %s, e2 = %s)\n", e1.toChars(), e2.toChars());
860851
if (e1.op == EXP.string_ && e2.op == EXP.string_)
861852
{
@@ -878,17 +869,11 @@ UnionExp Cmp(EXP op, Loc loc, Type type, Expression e1, Expression e2)
878869
cantExp(ue);
879870
return ue;
880871
}
881-
else if (e1.type.isReal())
882-
{
883-
r1 = e1.toReal();
884-
r2 = e2.toReal();
885-
goto L1;
886-
}
887-
else if (e1.type.isImaginary())
872+
else if (e1.type.isReal() || e1.type.isImaginary())
888873
{
889-
r1 = e1.toImaginary();
890-
r2 = e2.toImaginary();
891-
L1:
874+
const bool isReal = e1.type.isReal();
875+
real_t r1 = isReal ? e1.toReal() : e1.toImaginary();
876+
real_t r2 = isReal ? e2.toReal() : e2.toImaginary();
892877
n = realCmp(op, r1, r2);
893878
}
894879
else if (e1.type.isComplex())
@@ -938,32 +923,35 @@ UnionExp Cast(Loc loc, Type type, Type to, Expression e1)
938923
ue.exp().type = type;
939924
return ue;
940925
}
926+
UnionExp retExpType()
927+
{
928+
Expression ex = expType(to, e1);
929+
emplaceExp!(UnionExp)(&ue, ex);
930+
return ue;
931+
}
941932
if (e1.type.implicitConvTo(to) >= MATCH.constant || to.implicitConvTo(e1.type) >= MATCH.constant)
942933
{
943-
goto L1;
934+
return retExpType();
944935
}
945936
// Allow covariant converions of delegates
946937
// (Perhaps implicit conversion from pure to impure should be a MATCH.constant,
947938
// then we wouldn't need this extra check.)
948939
if (e1.type.toBasetype().ty == Tdelegate && e1.type.implicitConvTo(to) == MATCH.convert)
949940
{
950-
goto L1;
941+
return retExpType();
951942
}
952943
/* Allow casting from one string type to another
953944
*/
954945
if (e1.op == EXP.string_)
955946
{
956947
if (tb.ty == Tarray && typeb.ty == Tarray && tb.nextOf().size() == typeb.nextOf().size())
957948
{
958-
goto L1;
949+
return retExpType();
959950
}
960951
}
961952
if (e1.op == EXP.arrayLiteral && typeb == tb)
962953
{
963-
L1:
964-
Expression ex = expType(to, e1);
965-
emplaceExp!(UnionExp)(&ue, ex);
966-
return ue;
954+
return retExpType();
967955
}
968956
if (e1.isConst() != 1)
969957
{
@@ -1602,15 +1590,12 @@ UnionExp Cat(Loc loc, Type type, Expression e1, Expression e2)
16021590
assert(ue.exp().type);
16031591
return ue;
16041592
}
1605-
else if (e1.op == EXP.arrayLiteral && e2.op == EXP.null_ && t1.nextOf().equals(t2.nextOf()))
1593+
else if ((e1.op == EXP.arrayLiteral && e2.op == EXP.null_)
1594+
|| (e1.op == EXP.null_ && e2.op == EXP.arrayLiteral)
1595+
&& t1.nextOf().equals(t2.nextOf()))
16061596
{
1607-
e = e1;
1608-
goto L3;
1609-
}
1610-
else if (e1.op == EXP.null_ && e2.op == EXP.arrayLiteral && t1.nextOf().equals(t2.nextOf()))
1611-
{
1612-
e = e2;
1613-
L3:
1597+
e = (e1.op == EXP.arrayLiteral) ? e1 : e2;
1598+
16141599
// Concatenate the array with null
16151600
auto elems = copyElements(e);
16161601

@@ -1660,17 +1645,13 @@ UnionExp Cat(Loc loc, Type type, Expression e1, Expression e2)
16601645
assert(ue.exp().type);
16611646
return ue;
16621647
}
1663-
else if (e1.op == EXP.null_ && e2.op == EXP.string_)
1648+
else if ((e1.op == EXP.null_ && e2.op == EXP.string_) ||
1649+
(e1.op == EXP.string_ && e2.op == EXP.null_))
16641650
{
1665-
t = e1.type;
1666-
e = e2;
1667-
goto L1;
1668-
}
1669-
else if (e1.op == EXP.string_ && e2.op == EXP.null_)
1670-
{
1671-
e = e1;
1672-
t = e2.type;
1673-
L1:
1651+
const bool b = e1.op == EXP.null_;
1652+
t = b ? e1.type : e2.type;
1653+
e = b ? e2 : e1;
1654+
16741655
Type tb = t.toBasetype();
16751656
if (tb.ty == Tarray && tb.nextOf().equivalent(e.type))
16761657
{

0 commit comments

Comments
 (0)