Skip to content

Commit 8803ede

Browse files
committed
Place returns as needed without an extra block
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 4d4505e commit 8803ede

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ private void GeneratePropertySetter<T>(T decl,
898898
NewLine();
899899
WriteOpenBraceAndIndent();
900900

901-
this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true);
901+
this.GenerateMember(@class, c => GenerateFunctionSetter(c, property));
902902
}
903903
else if (decl is Variable)
904904
{
@@ -907,8 +907,7 @@ private void GeneratePropertySetter<T>(T decl,
907907
var var = decl as Variable;
908908
this.GenerateMember(@class, c => GenerateVariableSetter(
909909
c is ClassTemplateSpecialization ?
910-
c.Variables.First(v => v.Name == decl.Name) : var),
911-
true);
910+
c.Variables.First(v => v.Name == decl.Name) : var));
912911
}
913912
else if (decl is Field)
914913
{
@@ -925,7 +924,7 @@ c is ClassTemplateSpecialization ?
925924
PopBlock(NewLineKind.BeforeNextBlock);
926925
}
927926

928-
private void GenerateVariableSetter(Variable var)
927+
private bool GenerateVariableSetter(Variable var)
929928
{
930929
string ptr = GeneratePointerTo(var);
931930

@@ -957,6 +956,8 @@ private void GenerateVariableSetter(Variable var)
957956

958957
if (ctx.HasCodeBlock)
959958
UnindentAndWriteCloseBrace();
959+
960+
return true;
960961
}
961962

962963
private string GeneratePointerTo(Variable var)
@@ -991,7 +992,7 @@ private string GeneratePointerTo(Variable var)
991992
return ptr;
992993
}
993994

994-
private void GenerateFunctionSetter(Class @class, Property property)
995+
private bool GenerateFunctionSetter(Class @class, Property property)
995996
{
996997
var actualProperty = GetActualProperty(property, @class);
997998
if (actualProperty == null)
@@ -1000,8 +1001,7 @@ private void GenerateFunctionSetter(Class @class, Property property)
10001001
property.Name} missing from explicit specialization {
10011002
@class.Visit(TypePrinter)}."");");
10021003

1003-
AddBlock(new Block(BlockKind.Unreachable));
1004-
return;
1004+
return false;
10051005
}
10061006
property = actualProperty;
10071007

@@ -1010,6 +1010,7 @@ private void GenerateFunctionSetter(Class @class, Property property)
10101010
else
10111011
GenerateFunctionInProperty(@class, property.SetMethod, actualProperty,
10121012
new QualifiedType(new BuiltinType(PrimitiveType.Void)));
1013+
return true;
10131014
}
10141015

10151016
private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType)
@@ -1283,7 +1284,7 @@ c is ClassTemplateSpecialization ?
12831284
PopBlock(NewLineKind.BeforeNextBlock);
12841285
}
12851286

1286-
private void GenerateVariableGetter(Variable var)
1287+
private bool GenerateVariableGetter(Variable var)
12871288
{
12881289
string ptr = GeneratePointerTo(var);
12891290

@@ -1320,9 +1321,11 @@ private void GenerateVariableGetter(Variable var)
13201321

13211322
if (ctx.HasCodeBlock)
13221323
UnindentAndWriteCloseBrace();
1324+
1325+
return true;
13231326
}
13241327

1325-
private void GenerateFunctionGetter(Class @class, Property property)
1328+
private bool GenerateFunctionGetter(Class @class, Property property)
13261329
{
13271330
var actualProperty = GetActualProperty(property, @class);
13281331
if (actualProperty == null)
@@ -1331,8 +1334,7 @@ private void GenerateFunctionGetter(Class @class, Property property)
13311334
property.Name} missing from explicit specialization {
13321335
@class.Visit(TypePrinter)}."");");
13331336

1334-
AddBlock(new Block(BlockKind.Unreachable));
1335-
return;
1337+
return false;
13361338
}
13371339
QualifiedType type = default;
13381340
if (actualProperty != property ||
@@ -1343,6 +1345,7 @@ private void GenerateFunctionGetter(Class @class, Property property)
13431345
type = property.QualifiedType;
13441346
}
13451347
GenerateFunctionInProperty(@class, actualProperty.GetMethod, actualProperty, type);
1348+
return false;
13461349
}
13471350

13481351
private static Property GetActualProperty(Property property, Class c)
@@ -2307,9 +2310,9 @@ private void GenerateDisposeMethods(Class @class)
23072310
if (dtor.IsVirtual)
23082311
this.GenerateMember(@class, c => GenerateDestructorCall(
23092312
c is ClassTemplateSpecialization ?
2310-
c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor), true);
2313+
c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor));
23112314
else
2312-
this.GenerateMember(@class, c => GenerateMethodBody(c, dtor), true);
2315+
this.GenerateMember(@class, c => GenerateMethodBody(c, dtor));
23132316
if (@class.IsDependent || dtor.IsVirtual)
23142317
UnindentAndWriteCloseBrace();
23152318
else
@@ -2337,7 +2340,7 @@ c is ClassTemplateSpecialization ?
23372340
PopBlock(NewLineKind.BeforeNextBlock);
23382341
}
23392342

2340-
private void GenerateDestructorCall(Method dtor)
2343+
private bool GenerateDestructorCall(Method dtor)
23412344
{
23422345
var @class = (Class) dtor.Namespace;
23432346
GenerateVirtualFunctionCall(dtor, true);
@@ -2349,6 +2352,7 @@ private void GenerateDestructorCall(Method dtor)
23492352
GenerateInternalFunctionCall(dtor);
23502353
Unindent();
23512354
}
2355+
return true;
23522356
}
23532357

23542358
private void GenerateNativeConstructor(Class @class)
@@ -2669,7 +2673,7 @@ public void GenerateMethod(Method method, Class @class)
26692673
var isVoid = method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void) ||
26702674
method.IsConstructor;
26712675
this.GenerateMember(@class, c => GenerateMethodBody(
2672-
c, method, method.OriginalReturnType), isVoid);
2676+
c, method, method.OriginalReturnType));
26732677
}
26742678

26752679
SkipImpl:
@@ -2684,7 +2688,7 @@ public void GenerateMethod(Method method, Class @class)
26842688
PopBlock(NewLineKind.BeforeNextBlock);
26852689
}
26862690

2687-
private void GenerateMethodBody(Class @class, Method method,
2691+
private bool GenerateMethodBody(Class @class, Method method,
26882692
QualifiedType returnType = default(QualifiedType))
26892693
{
26902694
var specialization = @class as ClassTemplateSpecialization;
@@ -2698,17 +2702,15 @@ private void GenerateMethodBody(Class @class, Method method,
26982702
method.Name} missing from explicit specialization {
26992703
@class.Visit(TypePrinter)}."");");
27002704

2701-
AddBlock(new Block(BlockKind.Unreachable));
2702-
return;
2705+
return false;
27032706
}
27042707
if (specializedMethod.Ignore)
27052708
{
27062709
WriteLine($@"throw new MissingMethodException(""Method {
27072710
method.Name} ignored in specialization {
27082711
@class.Visit(TypePrinter)}."");");
27092712

2710-
AddBlock(new Block(BlockKind.Unreachable));
2711-
return;
2713+
return false;
27122714
}
27132715

27142716
method = specializedMethod;
@@ -2718,8 +2720,9 @@ private void GenerateMethodBody(Class @class, Method method,
27182720
if (method.IsConstructor)
27192721
{
27202722
GenerateClassConstructor(method, @class);
2723+
return true;
27212724
}
2722-
else if (method.IsOperator)
2725+
if (method.IsOperator)
27232726
{
27242727
GenerateOperator(method, returnType);
27252728
}
@@ -2755,6 +2758,8 @@ private void GenerateMethodBody(Class @class, Method method,
27552758
GenerateInternalFunctionCall(method);
27562759
}
27572760
}
2761+
2762+
return method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void);
27582763
}
27592764

27602765
private string OverloadParamNameWithDefValue(Parameter p, ref int index)
@@ -2843,12 +2848,13 @@ private void GenerateEqualsAndGetHashCode(Function method, Class @class)
28432848
}
28442849
}
28452850

2846-
private void GenerateGetHashCode(Class @class)
2851+
private bool GenerateGetHashCode(Class @class)
28472852
{
28482853
WriteLine($"if ({Helpers.InstanceIdentifier} == {TypePrinter.IntPtrType}.Zero)");
28492854
WriteLineIndent($"return {TypePrinter.IntPtrType}.Zero.GetHashCode();");
28502855
WriteLine($@"return (*({TypePrinter.PrintNative(@class)}*) {
28512856
Helpers.InstanceIdentifier}).GetHashCode();");
2857+
return false;
28522858
}
28532859

28542860
private void GenerateVirtualFunctionCall(Method method,

src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static void GenerateField(this CSharpSources gen, Class @class,
9595
}
9696

9797
public static void GenerateMember(this CSharpSources gen,
98-
Class @class, Action<Class> generate, bool isVoid = false)
98+
Class @class, Func<Class, bool> generate)
9999
{
100100
if (@class != null && @class.IsDependent)
101101
{
@@ -105,13 +105,12 @@ public static void GenerateMember(this CSharpSources gen,
105105
foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated))
106106
{
107107
WriteTemplateSpecializationCheck(gen, @class, specialization);
108-
gen.PushBlock(BlockKind.Block);
109108
gen.WriteOpenBraceAndIndent();
110-
generate(specialization);
111-
if (isVoid && !gen.ActiveBlock.FindBlocks(BlockKind.Unreachable).Any())
109+
if (generate(specialization))
110+
{
112111
gen.WriteLine("return;");
112+
}
113113
gen.UnindentAndWriteCloseBrace();
114-
gen.PopBlock();
115114
}
116115
ThrowException(gen, @class);
117116
}

src/Generator/Utils/BlockGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public enum BlockKind
3838
Event,
3939
Variable,
4040
Property,
41-
Unreachable,
4241
Field,
4342
VTableDelegate,
4443
Region,

0 commit comments

Comments
 (0)