From 8803ede83d32bf1156297a8460926154821b8b98 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Mon, 30 Aug 2021 13:35:38 +0300 Subject: [PATCH] Place returns as needed without an extra block Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpSources.cs | 50 +++++++++++-------- .../CSharp/CSharpSourcesExtensions.cs | 9 ++-- src/Generator/Utils/BlockGenerator.cs | 1 - 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 0244c89b6f..9211f90a59 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -898,7 +898,7 @@ private void GeneratePropertySetter(T decl, NewLine(); WriteOpenBraceAndIndent(); - this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true); + this.GenerateMember(@class, c => GenerateFunctionSetter(c, property)); } else if (decl is Variable) { @@ -907,8 +907,7 @@ private void GeneratePropertySetter(T decl, var var = decl as Variable; this.GenerateMember(@class, c => GenerateVariableSetter( c is ClassTemplateSpecialization ? - c.Variables.First(v => v.Name == decl.Name) : var), - true); + c.Variables.First(v => v.Name == decl.Name) : var)); } else if (decl is Field) { @@ -925,7 +924,7 @@ c is ClassTemplateSpecialization ? PopBlock(NewLineKind.BeforeNextBlock); } - private void GenerateVariableSetter(Variable var) + private bool GenerateVariableSetter(Variable var) { string ptr = GeneratePointerTo(var); @@ -957,6 +956,8 @@ private void GenerateVariableSetter(Variable var) if (ctx.HasCodeBlock) UnindentAndWriteCloseBrace(); + + return true; } private string GeneratePointerTo(Variable var) @@ -991,7 +992,7 @@ private string GeneratePointerTo(Variable var) return ptr; } - private void GenerateFunctionSetter(Class @class, Property property) + private bool GenerateFunctionSetter(Class @class, Property property) { var actualProperty = GetActualProperty(property, @class); if (actualProperty == null) @@ -1000,8 +1001,7 @@ private void GenerateFunctionSetter(Class @class, Property property) property.Name} missing from explicit specialization { @class.Visit(TypePrinter)}."");"); - AddBlock(new Block(BlockKind.Unreachable)); - return; + return false; } property = actualProperty; @@ -1010,6 +1010,7 @@ private void GenerateFunctionSetter(Class @class, Property property) else GenerateFunctionInProperty(@class, property.SetMethod, actualProperty, new QualifiedType(new BuiltinType(PrimitiveType.Void))); + return true; } private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType) @@ -1283,7 +1284,7 @@ c is ClassTemplateSpecialization ? PopBlock(NewLineKind.BeforeNextBlock); } - private void GenerateVariableGetter(Variable var) + private bool GenerateVariableGetter(Variable var) { string ptr = GeneratePointerTo(var); @@ -1320,9 +1321,11 @@ private void GenerateVariableGetter(Variable var) if (ctx.HasCodeBlock) UnindentAndWriteCloseBrace(); + + return true; } - private void GenerateFunctionGetter(Class @class, Property property) + private bool GenerateFunctionGetter(Class @class, Property property) { var actualProperty = GetActualProperty(property, @class); if (actualProperty == null) @@ -1331,8 +1334,7 @@ private void GenerateFunctionGetter(Class @class, Property property) property.Name} missing from explicit specialization { @class.Visit(TypePrinter)}."");"); - AddBlock(new Block(BlockKind.Unreachable)); - return; + return false; } QualifiedType type = default; if (actualProperty != property || @@ -1343,6 +1345,7 @@ private void GenerateFunctionGetter(Class @class, Property property) type = property.QualifiedType; } GenerateFunctionInProperty(@class, actualProperty.GetMethod, actualProperty, type); + return false; } private static Property GetActualProperty(Property property, Class c) @@ -2307,9 +2310,9 @@ private void GenerateDisposeMethods(Class @class) if (dtor.IsVirtual) this.GenerateMember(@class, c => GenerateDestructorCall( c is ClassTemplateSpecialization ? - c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor), true); + c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor)); else - this.GenerateMember(@class, c => GenerateMethodBody(c, dtor), true); + this.GenerateMember(@class, c => GenerateMethodBody(c, dtor)); if (@class.IsDependent || dtor.IsVirtual) UnindentAndWriteCloseBrace(); else @@ -2337,7 +2340,7 @@ c is ClassTemplateSpecialization ? PopBlock(NewLineKind.BeforeNextBlock); } - private void GenerateDestructorCall(Method dtor) + private bool GenerateDestructorCall(Method dtor) { var @class = (Class) dtor.Namespace; GenerateVirtualFunctionCall(dtor, true); @@ -2349,6 +2352,7 @@ private void GenerateDestructorCall(Method dtor) GenerateInternalFunctionCall(dtor); Unindent(); } + return true; } private void GenerateNativeConstructor(Class @class) @@ -2669,7 +2673,7 @@ public void GenerateMethod(Method method, Class @class) var isVoid = method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void) || method.IsConstructor; this.GenerateMember(@class, c => GenerateMethodBody( - c, method, method.OriginalReturnType), isVoid); + c, method, method.OriginalReturnType)); } SkipImpl: @@ -2684,7 +2688,7 @@ public void GenerateMethod(Method method, Class @class) PopBlock(NewLineKind.BeforeNextBlock); } - private void GenerateMethodBody(Class @class, Method method, + private bool GenerateMethodBody(Class @class, Method method, QualifiedType returnType = default(QualifiedType)) { var specialization = @class as ClassTemplateSpecialization; @@ -2698,8 +2702,7 @@ private void GenerateMethodBody(Class @class, Method method, method.Name} missing from explicit specialization { @class.Visit(TypePrinter)}."");"); - AddBlock(new Block(BlockKind.Unreachable)); - return; + return false; } if (specializedMethod.Ignore) { @@ -2707,8 +2710,7 @@ private void GenerateMethodBody(Class @class, Method method, method.Name} ignored in specialization { @class.Visit(TypePrinter)}."");"); - AddBlock(new Block(BlockKind.Unreachable)); - return; + return false; } method = specializedMethod; @@ -2718,8 +2720,9 @@ private void GenerateMethodBody(Class @class, Method method, if (method.IsConstructor) { GenerateClassConstructor(method, @class); + return true; } - else if (method.IsOperator) + if (method.IsOperator) { GenerateOperator(method, returnType); } @@ -2755,6 +2758,8 @@ private void GenerateMethodBody(Class @class, Method method, GenerateInternalFunctionCall(method); } } + + return method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void); } private string OverloadParamNameWithDefValue(Parameter p, ref int index) @@ -2843,12 +2848,13 @@ private void GenerateEqualsAndGetHashCode(Function method, Class @class) } } - private void GenerateGetHashCode(Class @class) + private bool GenerateGetHashCode(Class @class) { WriteLine($"if ({Helpers.InstanceIdentifier} == {TypePrinter.IntPtrType}.Zero)"); WriteLineIndent($"return {TypePrinter.IntPtrType}.Zero.GetHashCode();"); WriteLine($@"return (*({TypePrinter.PrintNative(@class)}*) { Helpers.InstanceIdentifier}).GetHashCode();"); + return false; } private void GenerateVirtualFunctionCall(Method method, diff --git a/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs b/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs index d3081ac2d7..da08ec4945 100644 --- a/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs +++ b/src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs @@ -95,7 +95,7 @@ public static void GenerateField(this CSharpSources gen, Class @class, } public static void GenerateMember(this CSharpSources gen, - Class @class, Action generate, bool isVoid = false) + Class @class, Func generate) { if (@class != null && @class.IsDependent) { @@ -105,13 +105,12 @@ public static void GenerateMember(this CSharpSources gen, foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated)) { WriteTemplateSpecializationCheck(gen, @class, specialization); - gen.PushBlock(BlockKind.Block); gen.WriteOpenBraceAndIndent(); - generate(specialization); - if (isVoid && !gen.ActiveBlock.FindBlocks(BlockKind.Unreachable).Any()) + if (generate(specialization)) + { gen.WriteLine("return;"); + } gen.UnindentAndWriteCloseBrace(); - gen.PopBlock(); } ThrowException(gen, @class); } diff --git a/src/Generator/Utils/BlockGenerator.cs b/src/Generator/Utils/BlockGenerator.cs index e20a1bfad8..0c83482e74 100644 --- a/src/Generator/Utils/BlockGenerator.cs +++ b/src/Generator/Utils/BlockGenerator.cs @@ -38,7 +38,6 @@ public enum BlockKind Event, Variable, Property, - Unreachable, Field, VTableDelegate, Region,