Skip to content

Commit d6fe4e2

Browse files
committed
[cleanup] Refactor GenC.VisitReturn.
1 parent b603bea commit d6fe4e2

File tree

4 files changed

+65
-61
lines changed

4 files changed

+65
-61
lines changed

GenC.fu

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,24 +2875,24 @@ public class GenC : GenCCpp
28752875
WriteDestructAll();
28762876
base.VisitReturn(statement);
28772877
}
2878-
else {
2879-
if (statement.Value is FuSymbolReference symbol && symbol.Symbol is FuVar local) {
2880-
if (this.VarsToDestruct.Contains(local)) {
2881-
// Optimization: avoid copy
2882-
WriteDestructAll(local);
2883-
Write("return ");
2884-
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
2885-
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument); // upcast, but don't AddRef
2886-
else
2887-
symbol.Accept(this, FuPriority.Argument);
2888-
WriteCharLine(';');
2889-
return;
2890-
}
2878+
else if (statement.Value is FuSymbolReference symbol && symbol.Symbol is FuVar local) {
2879+
if (this.VarsToDestruct.Contains(local)) {
2880+
// Optimization: avoid copy
2881+
WriteDestructAll(local);
2882+
Write("return ");
2883+
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
2884+
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument); // upcast, but don't AddRef
2885+
else
2886+
symbol.Accept(this, FuPriority.Argument);
2887+
WriteCharLine(';');
2888+
}
2889+
else {
28912890
// Local variable value doesn't depend on destructed variables
28922891
WriteDestructAll();
28932892
base.VisitReturn(statement);
2894-
return;
28952893
}
2894+
}
2895+
else {
28962896
WriteTemporaries(statement.Value);
28972897
EnsureChildBlock();
28982898
StartDefinition(this.CurrentMethod.Type, true, true);

libfut.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11837,26 +11837,28 @@ void GenC::visitReturn(const FuReturn * statement)
1183711837
else
1183811838
symbol->accept(this, FuPriority::argument);
1183911839
writeCharLine(';');
11840-
return;
1184111840
}
11842-
writeDestructAll();
11843-
GenCCpp::visitReturn(statement);
11844-
return;
11841+
else {
11842+
writeDestructAll();
11843+
GenCCpp::visitReturn(statement);
11844+
}
1184511845
}
11846-
writeTemporaries(statement->value.get());
11847-
ensureChildBlock();
11848-
startDefinition(this->currentMethod->type.get(), true, true);
11849-
write("returnValue = ");
11850-
writeCoerced(this->currentMethod->type.get(), statement->value.get(), FuPriority::argument);
11851-
writeCharLine(';');
11852-
cleanupTemporaries();
11853-
writeDestructAll();
11854-
if (throwingMethod != nullptr) {
11855-
startForwardThrow(throwingMethod);
11856-
write("returnValue");
11857-
endForwardThrow(throwingMethod);
11846+
else {
11847+
writeTemporaries(statement->value.get());
11848+
ensureChildBlock();
11849+
startDefinition(this->currentMethod->type.get(), true, true);
11850+
write("returnValue = ");
11851+
writeCoerced(this->currentMethod->type.get(), statement->value.get(), FuPriority::argument);
11852+
writeCharLine(';');
11853+
cleanupTemporaries();
11854+
writeDestructAll();
11855+
if (throwingMethod != nullptr) {
11856+
startForwardThrow(throwingMethod);
11857+
write("returnValue");
11858+
endForwardThrow(throwingMethod);
11859+
}
11860+
writeLine("return returnValue;");
1185811861
}
11859-
writeLine("return returnValue;");
1186011862
}
1186111863
}
1186211864
}

libfut.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12090,22 +12090,22 @@ internal override void VisitReturn(FuReturn statement)
1209012090
WriteDestructAll();
1209112091
base.VisitReturn(statement);
1209212092
}
12093-
else {
12094-
if (statement.Value is FuSymbolReference symbol && symbol.Symbol is FuVar local) {
12095-
if (this.VarsToDestruct.Contains(local)) {
12096-
WriteDestructAll(local);
12097-
Write("return ");
12098-
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
12099-
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument);
12100-
else
12101-
symbol.Accept(this, FuPriority.Argument);
12102-
WriteCharLine(';');
12103-
return;
12104-
}
12093+
else if (statement.Value is FuSymbolReference symbol && symbol.Symbol is FuVar local) {
12094+
if (this.VarsToDestruct.Contains(local)) {
12095+
WriteDestructAll(local);
12096+
Write("return ");
12097+
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
12098+
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument);
12099+
else
12100+
symbol.Accept(this, FuPriority.Argument);
12101+
WriteCharLine(';');
12102+
}
12103+
else {
1210512104
WriteDestructAll();
1210612105
base.VisitReturn(statement);
12107-
return;
1210812106
}
12107+
}
12108+
else {
1210912109
WriteTemporaries(statement.Value);
1211012110
EnsureChildBlock();
1211112111
StartDefinition(this.CurrentMethod.Type, true, true);

libfut.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12495,26 +12495,28 @@ export class GenC extends GenCCpp
1249512495
else
1249612496
symbol.accept(this, FuPriority.ARGUMENT);
1249712497
this.writeCharLine(59);
12498-
return;
1249912498
}
12500-
this.#writeDestructAll();
12501-
super.visitReturn(statement);
12502-
return;
12499+
else {
12500+
this.#writeDestructAll();
12501+
super.visitReturn(statement);
12502+
}
1250312503
}
12504-
this.writeTemporaries(statement.value);
12505-
this.ensureChildBlock();
12506-
this.#startDefinition(this.currentMethod.type, true, true);
12507-
this.write("returnValue = ");
12508-
this.writeCoerced(this.currentMethod.type, statement.value, FuPriority.ARGUMENT);
12509-
this.writeCharLine(59);
12510-
this.cleanupTemporaries();
12511-
this.#writeDestructAll();
12512-
if (throwingMethod != null) {
12513-
this.#startForwardThrow(throwingMethod);
12514-
this.write("returnValue");
12515-
this.#endForwardThrow(throwingMethod);
12504+
else {
12505+
this.writeTemporaries(statement.value);
12506+
this.ensureChildBlock();
12507+
this.#startDefinition(this.currentMethod.type, true, true);
12508+
this.write("returnValue = ");
12509+
this.writeCoerced(this.currentMethod.type, statement.value, FuPriority.ARGUMENT);
12510+
this.writeCharLine(59);
12511+
this.cleanupTemporaries();
12512+
this.#writeDestructAll();
12513+
if (throwingMethod != null) {
12514+
this.#startForwardThrow(throwingMethod);
12515+
this.write("returnValue");
12516+
this.#endForwardThrow(throwingMethod);
12517+
}
12518+
this.writeLine("return returnValue;");
1251612519
}
12517-
this.writeLine("return returnValue;");
1251812520
}
1251912521
}
1252012522
}

0 commit comments

Comments
 (0)