Browse Source

Place returns as needed without an extra block

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1632/head
Dimitar Dobrev 4 years ago
parent
commit
8803ede83d
  1. 50
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 9
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  3. 1
      src/Generator/Utils/BlockGenerator.cs

50
src/Generator/Generators/CSharp/CSharpSources.cs

@ -898,7 +898,7 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true); this.GenerateMember(@class, c => GenerateFunctionSetter(c, property));
} }
else if (decl is Variable) else if (decl is Variable)
{ {
@ -907,8 +907,7 @@ namespace CppSharp.Generators.CSharp
var var = decl as Variable; var var = decl as Variable;
this.GenerateMember(@class, c => GenerateVariableSetter( this.GenerateMember(@class, c => GenerateVariableSetter(
c is ClassTemplateSpecialization ? c is ClassTemplateSpecialization ?
c.Variables.First(v => v.Name == decl.Name) : var), c.Variables.First(v => v.Name == decl.Name) : var));
true);
} }
else if (decl is Field) else if (decl is Field)
{ {
@ -925,7 +924,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateVariableSetter(Variable var) private bool GenerateVariableSetter(Variable var)
{ {
string ptr = GeneratePointerTo(var); string ptr = GeneratePointerTo(var);
@ -957,6 +956,8 @@ namespace CppSharp.Generators.CSharp
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
return true;
} }
private string GeneratePointerTo(Variable var) private string GeneratePointerTo(Variable var)
@ -991,7 +992,7 @@ namespace CppSharp.Generators.CSharp
return ptr; return ptr;
} }
private void GenerateFunctionSetter(Class @class, Property property) private bool GenerateFunctionSetter(Class @class, Property property)
{ {
var actualProperty = GetActualProperty(property, @class); var actualProperty = GetActualProperty(property, @class);
if (actualProperty == null) if (actualProperty == null)
@ -1000,8 +1001,7 @@ namespace CppSharp.Generators.CSharp
property.Name} missing from explicit specialization { property.Name} missing from explicit specialization {
@class.Visit(TypePrinter)}."");"); @class.Visit(TypePrinter)}."");");
AddBlock(new Block(BlockKind.Unreachable)); return false;
return;
} }
property = actualProperty; property = actualProperty;
@ -1010,6 +1010,7 @@ namespace CppSharp.Generators.CSharp
else else
GenerateFunctionInProperty(@class, property.SetMethod, actualProperty, GenerateFunctionInProperty(@class, property.SetMethod, actualProperty,
new QualifiedType(new BuiltinType(PrimitiveType.Void))); new QualifiedType(new BuiltinType(PrimitiveType.Void)));
return true;
} }
private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType) private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldType)
@ -1283,7 +1284,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateVariableGetter(Variable var) private bool GenerateVariableGetter(Variable var)
{ {
string ptr = GeneratePointerTo(var); string ptr = GeneratePointerTo(var);
@ -1320,9 +1321,11 @@ namespace CppSharp.Generators.CSharp
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
return true;
} }
private void GenerateFunctionGetter(Class @class, Property property) private bool GenerateFunctionGetter(Class @class, Property property)
{ {
var actualProperty = GetActualProperty(property, @class); var actualProperty = GetActualProperty(property, @class);
if (actualProperty == null) if (actualProperty == null)
@ -1331,8 +1334,7 @@ namespace CppSharp.Generators.CSharp
property.Name} missing from explicit specialization { property.Name} missing from explicit specialization {
@class.Visit(TypePrinter)}."");"); @class.Visit(TypePrinter)}."");");
AddBlock(new Block(BlockKind.Unreachable)); return false;
return;
} }
QualifiedType type = default; QualifiedType type = default;
if (actualProperty != property || if (actualProperty != property ||
@ -1343,6 +1345,7 @@ namespace CppSharp.Generators.CSharp
type = property.QualifiedType; type = property.QualifiedType;
} }
GenerateFunctionInProperty(@class, actualProperty.GetMethod, actualProperty, type); GenerateFunctionInProperty(@class, actualProperty.GetMethod, actualProperty, type);
return false;
} }
private static Property GetActualProperty(Property property, Class c) private static Property GetActualProperty(Property property, Class c)
@ -2307,9 +2310,9 @@ namespace CppSharp.Generators.CSharp
if (dtor.IsVirtual) if (dtor.IsVirtual)
this.GenerateMember(@class, c => GenerateDestructorCall( this.GenerateMember(@class, c => GenerateDestructorCall(
c is ClassTemplateSpecialization ? c is ClassTemplateSpecialization ?
c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor), true); c.Methods.First(m => m.InstantiatedFrom == dtor) : dtor));
else else
this.GenerateMember(@class, c => GenerateMethodBody(c, dtor), true); this.GenerateMember(@class, c => GenerateMethodBody(c, dtor));
if (@class.IsDependent || dtor.IsVirtual) if (@class.IsDependent || dtor.IsVirtual)
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
else else
@ -2337,7 +2340,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateDestructorCall(Method dtor) private bool GenerateDestructorCall(Method dtor)
{ {
var @class = (Class) dtor.Namespace; var @class = (Class) dtor.Namespace;
GenerateVirtualFunctionCall(dtor, true); GenerateVirtualFunctionCall(dtor, true);
@ -2349,6 +2352,7 @@ namespace CppSharp.Generators.CSharp
GenerateInternalFunctionCall(dtor); GenerateInternalFunctionCall(dtor);
Unindent(); Unindent();
} }
return true;
} }
private void GenerateNativeConstructor(Class @class) private void GenerateNativeConstructor(Class @class)
@ -2669,7 +2673,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
var isVoid = method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void) || var isVoid = method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void) ||
method.IsConstructor; method.IsConstructor;
this.GenerateMember(@class, c => GenerateMethodBody( this.GenerateMember(@class, c => GenerateMethodBody(
c, method, method.OriginalReturnType), isVoid); c, method, method.OriginalReturnType));
} }
SkipImpl: SkipImpl:
@ -2684,7 +2688,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateMethodBody(Class @class, Method method, private bool GenerateMethodBody(Class @class, Method method,
QualifiedType returnType = default(QualifiedType)) QualifiedType returnType = default(QualifiedType))
{ {
var specialization = @class as ClassTemplateSpecialization; var specialization = @class as ClassTemplateSpecialization;
@ -2698,8 +2702,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
method.Name} missing from explicit specialization { method.Name} missing from explicit specialization {
@class.Visit(TypePrinter)}."");"); @class.Visit(TypePrinter)}."");");
AddBlock(new Block(BlockKind.Unreachable)); return false;
return;
} }
if (specializedMethod.Ignore) if (specializedMethod.Ignore)
{ {
@ -2707,8 +2710,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
method.Name} ignored in specialization { method.Name} ignored in specialization {
@class.Visit(TypePrinter)}."");"); @class.Visit(TypePrinter)}."");");
AddBlock(new Block(BlockKind.Unreachable)); return false;
return;
} }
method = specializedMethod; method = specializedMethod;
@ -2718,8 +2720,9 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
if (method.IsConstructor) if (method.IsConstructor)
{ {
GenerateClassConstructor(method, @class); GenerateClassConstructor(method, @class);
return true;
} }
else if (method.IsOperator) if (method.IsOperator)
{ {
GenerateOperator(method, returnType); GenerateOperator(method, returnType);
} }
@ -2755,6 +2758,8 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
GenerateInternalFunctionCall(method); GenerateInternalFunctionCall(method);
} }
} }
return method.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void);
} }
private string OverloadParamNameWithDefValue(Parameter p, ref int index) private string OverloadParamNameWithDefValue(Parameter p, ref int index)
@ -2843,12 +2848,13 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
} }
} }
private void GenerateGetHashCode(Class @class) private bool GenerateGetHashCode(Class @class)
{ {
WriteLine($"if ({Helpers.InstanceIdentifier} == {TypePrinter.IntPtrType}.Zero)"); WriteLine($"if ({Helpers.InstanceIdentifier} == {TypePrinter.IntPtrType}.Zero)");
WriteLineIndent($"return {TypePrinter.IntPtrType}.Zero.GetHashCode();"); WriteLineIndent($"return {TypePrinter.IntPtrType}.Zero.GetHashCode();");
WriteLine($@"return (*({TypePrinter.PrintNative(@class)}*) { WriteLine($@"return (*({TypePrinter.PrintNative(@class)}*) {
Helpers.InstanceIdentifier}).GetHashCode();"); Helpers.InstanceIdentifier}).GetHashCode();");
return false;
} }
private void GenerateVirtualFunctionCall(Method method, private void GenerateVirtualFunctionCall(Method method,

9
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -95,7 +95,7 @@ namespace CppSharp.Generators.CSharp
} }
public static void GenerateMember(this CSharpSources gen, 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) if (@class != null && @class.IsDependent)
{ {
@ -105,13 +105,12 @@ namespace CppSharp.Generators.CSharp
foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated)) foreach (var specialization in @class.Specializations.Where(s => s.IsGenerated))
{ {
WriteTemplateSpecializationCheck(gen, @class, specialization); WriteTemplateSpecializationCheck(gen, @class, specialization);
gen.PushBlock(BlockKind.Block);
gen.WriteOpenBraceAndIndent(); gen.WriteOpenBraceAndIndent();
generate(specialization); if (generate(specialization))
if (isVoid && !gen.ActiveBlock.FindBlocks(BlockKind.Unreachable).Any()) {
gen.WriteLine("return;"); gen.WriteLine("return;");
}
gen.UnindentAndWriteCloseBrace(); gen.UnindentAndWriteCloseBrace();
gen.PopBlock();
} }
ThrowException(gen, @class); ThrowException(gen, @class);
} }

1
src/Generator/Utils/BlockGenerator.cs

@ -38,7 +38,6 @@ namespace CppSharp
Event, Event,
Variable, Variable,
Property, Property,
Unreachable,
Field, Field,
VTableDelegate, VTableDelegate,
Region, Region,

Loading…
Cancel
Save