Skip to content

Commit

Permalink
[expressionsem.d] use early returns
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 18, 2024
1 parent 23b71f1 commit c15d0c6
Showing 1 changed file with 63 additions and 74 deletions.
137 changes: 63 additions & 74 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -327,33 +327,28 @@ StringExp semanticString(Scope *sc, Expression exp, const char* s)
return null;
}

auto se = e.toStringExp();
if (!se)
{
error(exp.loc, "`string` expected for %s, not `(%s)` of type `%s`",
s, exp.toChars(), exp.type.toChars());
return null;
}
return se;
if (auto se = e.toStringExp())
return se;
error(exp.loc, "`string` expected for %s, not `(%s)` of type `%s`",

Check warning on line 332 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L332

Added line #L332 was not covered by tests
s, exp.toChars(), exp.type.toChars());
return null;

Check warning on line 334 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L334

Added line #L334 was not covered by tests
}

/****************************************
* Convert string to char[].
*/
StringExp toUTF8(StringExp se, Scope* sc)
{
if (se.sz != 1)
{
// Convert to UTF-8 string
se.committed = false;
Expression e = castTo(se, sc, Type.tchar.arrayOf());
e = e.optimize(WANTvalue);
auto result = e.isStringExp();
assert(result);
assert(result.sz == 1);
return result;
}
return se;
if (se.sz == 1)
return se;
// Convert to UTF-8 string
se.committed = false;
Expression e = castTo(se, sc, Type.tchar.arrayOf());
e = e.optimize(WANTvalue);
auto result = e.isStringExp();
assert(result);
assert(result.sz == 1);
return result;
}
/********************************
* The type for a unary expression is incompatible.
Expand Down Expand Up @@ -523,84 +518,78 @@ private Expression checkOpAssignTypes(BinExp binExp, Scope* sc)
e2 = e2.castTo(sc, t1);
}
}
if (op == EXP.mulAssign)
if (op == EXP.mulAssign && t2.isFloating())
{
if (t2.isFloating())
if (t1.isReal())
{
if (t1.isReal())
if (t2.isImaginary() || t2.isComplex())
{
if (t2.isImaginary() || t2.isComplex())
{
e2 = e2.castTo(sc, t1);
}
}
else if (t1.isImaginary())
{
if (t2.isImaginary() || t2.isComplex())
{
switch (t1.ty)
{
case Timaginary32:
t2 = Type.tfloat32;
break;

case Timaginary64:
t2 = Type.tfloat64;
break;

case Timaginary80:
t2 = Type.tfloat80;
break;

default:
assert(0);
}
e2 = e2.castTo(sc, t2);
}
e2 = e2.castTo(sc, t1);

Check warning on line 527 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L527

Added line #L527 was not covered by tests
}
}
}
else if (op == EXP.divAssign)
{
if (t2.isImaginary())
else if (t1.isImaginary())
{
if (t1.isReal())
{
// x/iv = i(-x/v)
// Therefore, the result is 0
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1));
e2.type = t1;
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
}
else if (t1.isImaginary())
if (t2.isImaginary() || t2.isComplex())

Check warning on line 532 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L532

Added line #L532 was not covered by tests
{
Type t3;
switch (t1.ty)
{
case Timaginary32:
t3 = Type.tfloat32;
t2 = Type.tfloat32;

Check warning on line 537 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L537

Added line #L537 was not covered by tests
break;

case Timaginary64:
t3 = Type.tfloat64;
t2 = Type.tfloat64;

Check warning on line 541 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L541

Added line #L541 was not covered by tests
break;

case Timaginary80:
t3 = Type.tfloat80;
t2 = Type.tfloat80;

Check warning on line 545 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L545

Added line #L545 was not covered by tests
break;

default:
assert(0);
}
e2 = e2.castTo(sc, t3);
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;
e2 = e2.castTo(sc, t2);

Check warning on line 551 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L551

Added line #L551 was not covered by tests
}
}
}
else if (op == EXP.divAssign && t2.isImaginary())
{
if (t1.isReal())

Check warning on line 557 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L557

Added line #L557 was not covered by tests
{
// x/iv = i(-x/v)
// Therefore, the result is 0
e2 = new CommaExp(loc, e2, new RealExp(loc, CTFloat.zero, t1));
e2.type = t1;
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;

Check warning on line 565 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L561-L565

Added lines #L561 - L565 were not covered by tests
}
else if (t1.isImaginary())

Check warning on line 567 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L567

Added line #L567 was not covered by tests
{
Type t3;
switch (t1.ty)

Check warning on line 570 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L569-L570

Added lines #L569 - L570 were not covered by tests
{
case Timaginary32:
t3 = Type.tfloat32;
break;

Check warning on line 574 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L572-L574

Added lines #L572 - L574 were not covered by tests

case Timaginary64:
t3 = Type.tfloat64;
break;

Check warning on line 578 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L576-L578

Added lines #L576 - L578 were not covered by tests

case Timaginary80:
t3 = Type.tfloat80;
break;

Check warning on line 582 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L580-L582

Added lines #L580 - L582 were not covered by tests

default:
assert(0);
}
e2 = e2.castTo(sc, t3);
Expression e = new AssignExp(loc, e1, e2);
e.type = t1;
return e;

Check warning on line 590 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L587-L590

Added lines #L587 - L590 were not covered by tests
}
}
else if (op == EXP.modAssign)
{
if (t2.isComplex())
Expand Down

0 comments on commit c15d0c6

Please sign in to comment.