Skip to content

Commit

Permalink
[clone.d] remove some gotos, reduce nesting (#16928)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator authored Oct 5, 2024
1 parent fecc140 commit e552871
Showing 1 changed file with 72 additions and 60 deletions.
132 changes: 72 additions & 60 deletions compiler/src/dmd/clone.d
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,26 @@ FuncDeclaration buildOpAssign(StructDeclaration sd, Scope* sc)
*/
bool needOpEquals(StructDeclaration sd)
{
bool dontneed()
{
//printf("\tdontneed\n");
return false;
}
bool need()
{
//printf("\tneed\n");
return true;
}
//printf("StructDeclaration::needOpEquals() %s\n", sd.toChars());
if (sd.isUnionDeclaration())
{
/* If a union has only one field, treat it like a struct
*/
if (sd.fields.length != 1)
goto Ldontneed;
return dontneed();
}
if (sd.hasIdentityEquals)
goto Lneed;
return need();
/* If any of the fields has an opEquals, then we
* need it too.
*/
Expand All @@ -443,28 +453,23 @@ bool needOpEquals(StructDeclaration sd)
if (ts.sym.isUnionDeclaration() && ts.sym.fields.length != 1)
continue;
if (needOpEquals(ts.sym))
goto Lneed;
return need();
}
if (tvbase.isFloating())
{
// This is necessray for:
// 1. comparison of +0.0 and -0.0 should be true.
// 2. comparison of NANs should be false always.
goto Lneed;
return need();
}
if (tvbase.ty == Tarray)
goto Lneed;
return need();
if (tvbase.ty == Taarray)
goto Lneed;
return need();
if (tvbase.ty == Tclass)
goto Lneed;
return need();
}
Ldontneed:
//printf("\tdontneed\n");
return false;
Lneed:
//printf("\tneed\n");
return true;
return dontneed();
}

/*******************************************
Expand All @@ -477,48 +482,50 @@ Lneed:
private FuncDeclaration hasIdentityOpEquals(AggregateDeclaration ad, Scope* sc)
{
FuncDeclaration f;
if (Dsymbol eq = search_function(ad, Id.eq))
Dsymbol eq = search_function(ad, Id.eq);
if (!eq)
return null;

/* check identity opEquals exists
*/
scope er = new NullExp(ad.loc, null); // dummy rvalue
scope el = new IdentifierExp(ad.loc, Id.p); // dummy lvalue
auto a = new Expressions(1);

bool hasIt(Type tthis)
{
/* check identity opEquals exists
*/
scope er = new NullExp(ad.loc, null); // dummy rvalue
scope el = new IdentifierExp(ad.loc, Id.p); // dummy lvalue
auto a = new Expressions(1);
const errors = global.startGagging(); // Do not report errors, even if the template opAssign fbody makes it
sc = sc.push();
sc.tinst = null;
sc.minst = null;

bool hasIt(Type tthis)
FuncDeclaration rfc(Expression e)
{
const errors = global.startGagging(); // Do not report errors, even if the template opAssign fbody makes it
sc = sc.push();
sc.tinst = null;
sc.minst = null;

FuncDeclaration rfc(Expression e)
{
(*a)[0] = e;
(*a)[0].type = tthis;
return resolveFuncCall(ad.loc, sc, eq, null, tthis, ArgumentList(a), FuncResolveFlag.quiet);
}
(*a)[0] = e;
(*a)[0].type = tthis;
return resolveFuncCall(ad.loc, sc, eq, null, tthis, ArgumentList(a), FuncResolveFlag.quiet);
}

f = rfc(er);
if (!f)
f = rfc(el);
f = rfc(er);
if (!f)
f = rfc(el);

sc = sc.pop();
global.endGagging(errors);
sc = sc.pop();
global.endGagging(errors);

return f !is null;
}
return f !is null;
}

if (hasIt(ad.type) ||
hasIt(ad.type.constOf()) ||
hasIt(ad.type.immutableOf()) ||
hasIt(ad.type.sharedOf()) ||
hasIt(ad.type.sharedConstOf()))
{
if (f.errors)
return null;
}
if (hasIt(ad.type) ||
hasIt(ad.type.constOf()) ||
hasIt(ad.type.immutableOf()) ||
hasIt(ad.type.sharedOf()) ||
hasIt(ad.type.sharedConstOf()))
{
if (f.errors)
return null;
}

return f;
}

Expand Down Expand Up @@ -756,11 +763,21 @@ FuncDeclaration buildXopCmp(StructDeclaration sd, Scope* sc)
*/
private bool needToHash(StructDeclaration sd)
{
bool dontneed()
{
//printf("\tdontneed\n");
return false;
}
bool need()
{
//printf("\tneed\n");
return true;
}
//printf("StructDeclaration::needToHash() %s\n", sd.toChars());
if (sd.isUnionDeclaration())
goto Ldontneed;
return dontneed();
if (sd.xhash)
goto Lneed;
return need();

/* If any of the fields has an toHash, then we
* need it too.
Expand All @@ -778,28 +795,23 @@ private bool needToHash(StructDeclaration sd)
if (ts.sym.isUnionDeclaration())
continue;
if (needToHash(ts.sym))
goto Lneed;
return need();
}
if (tvbase.isFloating())
{
/* This is necessary because comparison of +0.0 and -0.0 should be true,
* i.e. not a bit compare.
*/
goto Lneed;
return need();
}
if (tvbase.ty == Tarray)
goto Lneed;
return need();
if (tvbase.ty == Taarray)
goto Lneed;
return need();
if (tvbase.ty == Tclass)
goto Lneed;
return need();
}
Ldontneed:
//printf("\tdontneed\n");
return false;
Lneed:
//printf("\tneed\n");
return true;
return dontneed();
}

/******************************************
Expand Down

0 comments on commit e552871

Please sign in to comment.