From e552871f6b341ad2deede7ccddbf2d3d6a7520b1 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Sat, 5 Oct 2024 08:27:06 +0800 Subject: [PATCH] [clone.d] remove some gotos, reduce nesting (#16928) --- compiler/src/dmd/clone.d | 132 +++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 60 deletions(-) diff --git a/compiler/src/dmd/clone.d b/compiler/src/dmd/clone.d index 68b04f51144..19987f106ad 100644 --- a/compiler/src/dmd/clone.d +++ b/compiler/src/dmd/clone.d @@ -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. */ @@ -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(); } /******************************************* @@ -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; } @@ -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. @@ -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(); } /******************************************