Skip to content

Commit

Permalink
Place returns as needed without an extra block
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Aug 30, 2021
1 parent 4d4505e commit 8803ede
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
50 changes: 28 additions & 22 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ private void GeneratePropertySetter<T>(T decl,
NewLine();
WriteOpenBraceAndIndent();

this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true);
this.GenerateMember(@class, c => GenerateFunctionSetter(c, property));
}
else if (decl is Variable)
{
Expand All @@ -907,8 +907,7 @@ private void GeneratePropertySetter<T>(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)
{
Expand All @@ -925,7 +924,7 @@ c is ClassTemplateSpecialization ?
PopBlock(NewLineKind.BeforeNextBlock);
}

private void GenerateVariableSetter(Variable var)
private bool GenerateVariableSetter(Variable var)
{
string ptr = GeneratePointerTo(var);

Expand Down Expand Up @@ -957,6 +956,8 @@ private void GenerateVariableSetter(Variable var)

if (ctx.HasCodeBlock)
UnindentAndWriteCloseBrace();

return true;
}

private string GeneratePointerTo(Variable var)
Expand Down Expand Up @@ -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)
Expand All @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -1283,7 +1284,7 @@ c is ClassTemplateSpecialization ?
PopBlock(NewLineKind.BeforeNextBlock);
}

private void GenerateVariableGetter(Variable var)
private bool GenerateVariableGetter(Variable var)
{
string ptr = GeneratePointerTo(var);

Expand Down Expand Up @@ -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)
Expand All @@ -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 ||
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -2349,6 +2352,7 @@ private void GenerateDestructorCall(Method dtor)
GenerateInternalFunctionCall(dtor);
Unindent();
}
return true;
}

private void GenerateNativeConstructor(Class @class)
Expand Down Expand Up @@ -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:
Expand All @@ -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;
Expand All @@ -2698,17 +2702,15 @@ 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)
{
WriteLine($@"throw new MissingMethodException(""Method {
method.Name} ignored in specialization {
@class.Visit(TypePrinter)}."");");

AddBlock(new Block(BlockKind.Unreachable));
return;
return false;
}

method = specializedMethod;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void GenerateField(this CSharpSources gen, Class @class,
}

public static void GenerateMember(this CSharpSources gen,
Class @class, Action<Class> generate, bool isVoid = false)
Class @class, Func<Class, bool> generate)
{
if (@class != null && @class.IsDependent)
{
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/Generator/Utils/BlockGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public enum BlockKind
Event,
Variable,
Property,
Unreachable,
Field,
VTableDelegate,
Region,
Expand Down

0 comments on commit 8803ede

Please sign in to comment.