Browse Source

Renamed members related to indentation for more clarity.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1129/head
Dimitar Dobrev 7 years ago committed by João Matos
parent
commit
b7d3d36ff1
  1. 4
      src/Core/Diagnostics.cs
  2. 72
      src/Generator/Generators/CLI/CLIHeaders.cs
  3. 14
      src/Generator/Generators/CLI/CLIMarshal.cs
  4. 106
      src/Generator/Generators/CLI/CLISources.cs
  5. 36
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  6. 276
      src/Generator/Generators/CSharp/CSharpSources.cs
  7. 8
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  8. 10
      src/Generator/Generators/Marshal.cs
  9. 12
      src/Generator/Types/Std/Stdlib.cs
  10. 36
      src/Generator/Utils/BlockGenerator.cs
  11. 36
      src/Generator/Utils/TextGenerator.cs
  12. 4
      src/Generator/Utils/Utils.cs

4
src/Core/Diagnostics.cs

@ -117,8 +117,8 @@ namespace CppSharp
if (info.Kind < Level) if (info.Kind < Level)
return; return;
var currentIndent = Indents.Sum(); var currentIndentation = Indents.Sum();
var message = new string(' ', currentIndent) + info.Message; var message = new string(' ', currentIndentation) + info.Message;
if(info.Kind == DiagnosticKind.Error) if(info.Kind == DiagnosticKind.Error)
{ {

72
src/Generator/Generators/CLI/CLIHeaders.cs

@ -194,14 +194,14 @@ namespace CppSharp.Generators.CLI
WriteLine("namespace {0}", isTopLevel WriteLine("namespace {0}", isTopLevel
? @namespace.TranslationUnit.Module.OutputNamespace ? @namespace.TranslationUnit.Module.OutputNamespace
: @namespace.Name); : @namespace.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
GenerateDeclContext(@namespace); GenerateDeclContext(@namespace);
if (generateNamespace) if (generateNamespace)
{ {
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
} }
@ -224,7 +224,7 @@ namespace CppSharp.Generators.CLI
WriteLine("public ref class {0}", TranslationUnit.FileNameWithoutExtension); WriteLine("public ref class {0}", TranslationUnit.FileNameWithoutExtension);
WriteLine("{"); WriteLine("{");
WriteLine("public:"); WriteLine("public:");
PushIndent(); Indent();
// Generate all the function declarations for the module. // Generate all the function declarations for the module.
foreach (var function in decl.Functions) foreach (var function in decl.Functions)
@ -232,7 +232,7 @@ namespace CppSharp.Generators.CLI
GenerateFunction(function); GenerateFunction(function);
} }
PopIndent(); Unindent();
WriteLine("};"); WriteLine("};");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -259,9 +259,9 @@ namespace CppSharp.Generators.CLI
NewLine(); NewLine();
// Process the nested types. // Process the nested types.
PushIndent(); Indent();
GenerateDeclContext(@class); GenerateDeclContext(@class);
PopIndent(); Unindent();
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
@ -308,20 +308,20 @@ namespace CppSharp.Generators.CLI
{ {
WriteLineIndent("property {0} NativePtr;", nativeType); WriteLineIndent("property {0} NativePtr;", nativeType);
PushIndent(); Indent();
WriteLine("property System::IntPtr {0}", Helpers.InstanceIdentifier); WriteLine("property System::IntPtr {0}", Helpers.InstanceIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("virtual System::IntPtr get();"); WriteLine("virtual System::IntPtr get();");
WriteLine("virtual void set(System::IntPtr instance);"); WriteLine("virtual void set(System::IntPtr instance);");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
PopIndent(); Unindent();
} }
public void GenerateClassGenericMethods(Class @class) public void GenerateClassGenericMethods(Class @class)
{ {
PushIndent(); Indent();
foreach (var template in @class.Templates) foreach (var template in @class.Templates)
{ {
if (!template.IsGenerated) continue; if (!template.IsGenerated) continue;
@ -368,7 +368,7 @@ namespace CppSharp.Generators.CLI
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
PopIndent(); Unindent();
} }
public void GenerateClassConstructors(Class @class, string nativeType) public void GenerateClassConstructors(Class @class, string nativeType)
@ -376,7 +376,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsStatic) if (@class.IsStatic)
return; return;
PushIndent(); Indent();
// Output a default constructor that takes the native pointer. // Output a default constructor that takes the native pointer.
WriteLine("{0}({1} native);", @class.Name, nativeType); WriteLine("{0}({1} native);", @class.Name, nativeType);
@ -407,7 +407,7 @@ namespace CppSharp.Generators.CLI
} }
} }
PopIndent(); Unindent();
} }
private void GenerateClassDestructor(Class @class) private void GenerateClassDestructor(Class @class)
@ -436,7 +436,7 @@ namespace CppSharp.Generators.CLI
} }
} }
PushIndent(); Indent();
// check for value types because some of the ignored fields may back properties; // check for value types because some of the ignored fields may back properties;
// not the case for ref types because the NativePtr pattern is used there // not the case for ref types because the NativePtr pattern is used there
foreach (var field in @class.Fields.Where(f => !ASTUtils.CheckIgnoreField(f))) foreach (var field in @class.Fields.Where(f => !ASTUtils.CheckIgnoreField(f)))
@ -447,7 +447,7 @@ namespace CppSharp.Generators.CLI
GenerateField(@class, field); GenerateField(@class, field);
} }
} }
PopIndent(); Unindent();
} }
private void GenerateField(Class @class, Field field) private void GenerateField(Class @class, Field field)
@ -473,7 +473,7 @@ namespace CppSharp.Generators.CLI
var cppArgs = cppTypePrinter.VisitParameters(@event.Parameters, hasNames: true); var cppArgs = cppTypePrinter.VisitParameters(@event.Parameters, hasNames: true);
WriteLine("private:"); WriteLine("private:");
PushIndent(); Indent();
var delegateName = string.Format("_{0}Delegate", @event.Name); var delegateName = string.Format("_{0}Delegate", @event.Name);
WriteLine("delegate void {0}({1});", delegateName, cppArgs); WriteLine("delegate void {0}({1});", delegateName, cppArgs);
@ -482,12 +482,12 @@ namespace CppSharp.Generators.CLI
WriteLine("void _{0}Raise({1});", @event.Name, cppArgs); WriteLine("void _{0}Raise({1});", @event.Name, cppArgs);
WriteLine("{0} _{1};", @event.Type, @event.Name); WriteLine("{0} _{1};", @event.Type, @event.Name);
PopIndent(); Unindent();
WriteLine("public:"); WriteLine("public:");
PushIndent(); Indent();
WriteLine("event {0} {1}", @event.Type, @event.Name); WriteLine("event {0} {1}", @event.Type, @event.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("void add({0} evt);", @event.Type); WriteLine("void add({0} evt);", @event.Type);
WriteLine("void remove({0} evt);", @event.Type); WriteLine("void remove({0} evt);", @event.Type);
@ -496,8 +496,8 @@ namespace CppSharp.Generators.CLI
var cliArgs = cliTypePrinter.VisitParameters(@event.Parameters, hasNames: true); var cliArgs = cliTypePrinter.VisitParameters(@event.Parameters, hasNames: true);
WriteLine("void raise({0});", cliArgs); WriteLine("void raise({0});", cliArgs);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopIndent(); Unindent();
} }
} }
@ -506,7 +506,7 @@ namespace CppSharp.Generators.CLI
if (methods.Count == 0) if (methods.Count == 0)
return; return;
PushIndent(); Indent();
var @class = (Class) methods[0].Namespace; var @class = (Class) methods[0].Namespace;
@ -535,12 +535,12 @@ namespace CppSharp.Generators.CLI
foreach(var method in staticMethods) foreach(var method in staticMethods)
GenerateMethod(method); GenerateMethod(method);
PopIndent(); Unindent();
} }
public void GenerateClassVariables(Class @class) public void GenerateClassVariables(Class @class)
{ {
PushIndent(); Indent();
foreach(var variable in @class.Variables) foreach(var variable in @class.Variables)
{ {
@ -555,7 +555,7 @@ namespace CppSharp.Generators.CLI
WriteLine("static property {0} {1}", type, variable.Name); WriteLine("static property {0} {1}", type, variable.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0} get();", type); WriteLine("{0} get();", type);
@ -564,12 +564,12 @@ namespace CppSharp.Generators.CLI
if (!qualifiedType.Qualifiers.IsConst) if (!qualifiedType.Qualifiers.IsConst)
WriteLine("void set({0});", type); WriteLine("void set({0});", type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
PopIndent(); Unindent();
} }
public override void GenerateClassSpecifier(Class @class) public override void GenerateClassSpecifier(Class @class)
@ -612,7 +612,7 @@ namespace CppSharp.Generators.CLI
} }
} }
PushIndent(); Indent();
foreach (var prop in @class.Properties.Where( foreach (var prop in @class.Properties.Where(
prop => !ASTUtils.CheckIgnoreProperty(prop) && !TypeIgnored(prop.Type))) prop => !ASTUtils.CheckIgnoreProperty(prop) && !TypeIgnored(prop.Type)))
{ {
@ -625,7 +625,7 @@ namespace CppSharp.Generators.CLI
GenerateDeclarationCommon(prop); GenerateDeclarationCommon(prop);
GenerateProperty(prop); GenerateProperty(prop);
} }
PopIndent(); Unindent();
} }
public void GenerateIndexer(Property property) public void GenerateIndexer(Property property)
@ -636,7 +636,7 @@ namespace CppSharp.Generators.CLI
var indexParameterType = indexParameter.QualifiedType.Visit(TypePrinter); var indexParameterType = indexParameter.QualifiedType.Visit(TypePrinter);
WriteLine("property {0} default[{1}]", type, indexParameterType); WriteLine("property {0} default[{1}]", type, indexParameterType);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (property.HasGetter) if (property.HasGetter)
WriteLine("{0} get({1} {2});", type, indexParameterType, indexParameter.Name); WriteLine("{0} get({1} {2});", type, indexParameterType, indexParameter.Name);
@ -644,7 +644,7 @@ namespace CppSharp.Generators.CLI
if (property.HasSetter) if (property.HasSetter)
WriteLine("void set({1} {2}, {0} value);", type, indexParameterType, indexParameter.Name); WriteLine("void set({1} {2}, {0} value);", type, indexParameterType, indexParameter.Name);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
public void GenerateProperty(Property property) public void GenerateProperty(Property property)
@ -665,7 +665,7 @@ namespace CppSharp.Generators.CLI
else else
{ {
WriteLine("property {0} {1}", type, property.Name); WriteLine("property {0} {1}", type, property.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (property.HasGetter) if (property.HasGetter)
WriteLine("{0} get();", type); WriteLine("{0} get();", type);
@ -673,7 +673,7 @@ namespace CppSharp.Generators.CLI
if (property.HasSetter) if (property.HasSetter)
WriteLine("void set({0});", type); WriteLine("void set({0});", type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -835,11 +835,11 @@ namespace CppSharp.Generators.CLI
Write(" : {0}", typeName); Write(" : {0}", typeName);
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
GenerateEnumItems(@enum); GenerateEnumItems(@enum);
PopIndent(); Unindent();
WriteLine("};"); WriteLine("};");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);

14
src/Generator/Generators/CLI/CLIMarshal.cs

@ -47,7 +47,7 @@ namespace CppSharp.Generators.CLI
string value = Generator.GeneratedIdentifier("array") + Context.ParameterIndex; string value = Generator.GeneratedIdentifier("array") + Context.ParameterIndex;
Context.Before.WriteLine("cli::array<{0}>^ {1} = nullptr;", array.Type, value, array.Size); Context.Before.WriteLine("cli::array<{0}>^ {1} = nullptr;", array.Type, value, array.Size);
Context.Before.WriteLine("if ({0} != 0)", Context.ReturnVarName); Context.Before.WriteLine("if ({0} != 0)", Context.ReturnVarName);
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
Context.Before.WriteLine("{0} = gcnew cli::array<{1}>({2});", value, array.Type, array.Size); Context.Before.WriteLine("{0} = gcnew cli::array<{1}>({2});", value, array.Type, array.Size);
Context.Before.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); Context.Before.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void)) if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
@ -55,7 +55,7 @@ namespace CppSharp.Generators.CLI
value, Context.ReturnVarName); value, Context.ReturnVarName);
else else
Context.Before.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName); Context.Before.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName);
Context.Before.WriteCloseBraceIndent(); Context.Before.UnindentAndWriteCloseBrace();
Context.Return.Write(value); Context.Return.Write(value);
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
@ -468,12 +468,12 @@ namespace CppSharp.Generators.CLI
{ {
var supportBefore = Context.Before; var supportBefore = Context.Before;
supportBefore.WriteLine("if ({0} != nullptr)", Context.ArgName); supportBefore.WriteLine("if ({0} != nullptr)", Context.ArgName);
supportBefore.WriteStartBraceIndent(); supportBefore.WriteOpenBraceAndIndent();
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};", supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};",
Context.ReturnVarName, Context.ArgName, Context.ReturnVarName, Context.ArgName,
array.Type.IsPointerToPrimitiveType(PrimitiveType.Void) ? ".ToPointer()" : string.Empty); array.Type.IsPointerToPrimitiveType(PrimitiveType.Void) ? ".ToPointer()" : string.Empty);
supportBefore.WriteCloseBraceIndent(); supportBefore.UnindentAndWriteCloseBrace();
} }
break; break;
default: default:
@ -775,7 +775,7 @@ namespace CppSharp.Generators.CLI
var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name, var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
property.Name); property.Name);
var marshalCtx = new MarshalContext(Context.Context, Context.Indent) var marshalCtx = new MarshalContext(Context.Context, Context.Indentation)
{ {
ArgName = fieldRef, ArgName = fieldRef,
ParameterIndex = Context.ParameterIndex++, ParameterIndex = Context.ParameterIndex++,
@ -799,14 +799,14 @@ namespace CppSharp.Generators.CLI
if (isRef) if (isRef)
{ {
Context.Before.WriteLine("if ({0} != nullptr)", fieldRef); Context.Before.WriteLine("if ({0} != nullptr)", fieldRef);
Context.Before.PushIndent(); Context.Before.Indent();
} }
Context.Before.WriteLine("{0}.{1} = {2};", marshalVar, Context.Before.WriteLine("{0}.{1} = {2};", marshalVar,
property.Field.OriginalName, marshal.Context.Return); property.Field.OriginalName, marshal.Context.Return);
if (isRef) if (isRef)
Context.Before.PopIndent(); Context.Before.Unindent();
} }
public override bool VisitFieldDecl(Field field) public override bool VisitFieldDecl(Field field)

106
src/Generator/Generators/CLI/CLISources.cs

@ -140,18 +140,18 @@ namespace CppSharp.Generators.CLI
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
WriteLine("System::IntPtr {0}::{1}::get()", WriteLine("System::IntPtr {0}::{1}::get()",
qualifiedIdentifier, Helpers.InstanceIdentifier); qualifiedIdentifier, Helpers.InstanceIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("return System::IntPtr(NativePtr);"); WriteLine("return System::IntPtr(NativePtr);");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
WriteLine("void {0}::{1}::set(System::IntPtr object)", WriteLine("void {0}::{1}::set(System::IntPtr object)",
qualifiedIdentifier, Helpers.InstanceIdentifier); qualifiedIdentifier, Helpers.InstanceIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
WriteLine("NativePtr = ({0})object.ToPointer();", nativeType); WriteLine("NativePtr = ({0})object.ToPointer();", nativeType);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -247,7 +247,7 @@ namespace CppSharp.Generators.CLI
PushBlock(BlockKind.Destructor); PushBlock(BlockKind.Destructor);
WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name); WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (CLIGenerator.ShouldGenerateClassNativeField(@class)) if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{ {
@ -256,14 +256,14 @@ namespace CppSharp.Generators.CLI
else if (@class.HasNonTrivialDestructor) else if (@class.HasNonTrivialDestructor)
{ {
WriteLine("if (NativePtr)"); WriteLine("if (NativePtr)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("auto __nativePtr = NativePtr;"); WriteLine("auto __nativePtr = NativePtr;");
WriteLine("NativePtr = 0;"); WriteLine("NativePtr = 0;");
WriteLine("delete (::{0}*) __nativePtr;", @class.QualifiedOriginalName); WriteLine("delete (::{0}*) __nativePtr;", @class.QualifiedOriginalName);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -273,12 +273,12 @@ namespace CppSharp.Generators.CLI
PushBlock(BlockKind.Finalizer); PushBlock(BlockKind.Finalizer);
WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name); WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (CLIGenerator.ShouldGenerateClassNativeField(@class)) if (CLIGenerator.ShouldGenerateClassNativeField(@class))
WriteLine("delete NativePtr;"); WriteLine("delete NativePtr;");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -307,12 +307,12 @@ namespace CppSharp.Generators.CLI
QualifiedIdentifier(function.Namespace), function.Name, QualifiedIdentifier(function.Namespace), function.Name,
GenerateParametersList(function.Parameters)); GenerateParametersList(function.Parameters));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var @class = function.Namespace as Class; var @class = function.Namespace as Class;
GenerateFunctionCall(function, @class); GenerateFunctionCall(function, @class);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -367,7 +367,7 @@ namespace CppSharp.Generators.CLI
WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class),
name, string.Join(", ", args)); name, string.Join(", ", args));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (decl is Function && !isIndexer) if (decl is Function && !isIndexer)
{ {
@ -380,7 +380,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsValueType && decl is Field) if (@class.IsValueType && decl is Field)
{ {
WriteLine("{0} = value;", decl.Name); WriteLine("{0} = value;", decl.Name);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
return; return;
} }
@ -396,7 +396,7 @@ namespace CppSharp.Generators.CLI
else else
variable = $"((::{@class.QualifiedOriginalName}*)NativePtr)->{decl.OriginalName}"; variable = $"((::{@class.QualifiedOriginalName}*)NativePtr)->{decl.OriginalName}";
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
@ -408,7 +408,7 @@ namespace CppSharp.Generators.CLI
if (isIndexer) if (isIndexer)
{ {
var ctx2 = new MarshalContext(Context, CurrentIndent) var ctx2 = new MarshalContext(Context, CurrentIndentation)
{ {
Parameter = indexParameter, Parameter = indexParameter,
ArgName = indexParameter.Name ArgName = indexParameter.Name
@ -432,7 +432,7 @@ namespace CppSharp.Generators.CLI
} }
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
} }
@ -456,7 +456,7 @@ namespace CppSharp.Generators.CLI
WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class), WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class),
name, string.Join(", ", args)); name, string.Join(", ", args));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (decl is Function) if (decl is Function)
{ {
@ -471,7 +471,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsValueType && decl is Field) if (@class.IsValueType && decl is Field)
{ {
WriteLine("return {0};", decl.Name); WriteLine("return {0};", decl.Name);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
return; return;
} }
@ -483,7 +483,7 @@ namespace CppSharp.Generators.CLI
variable = string.Format("((::{0}*)NativePtr)->{1}", variable = string.Format("((::{0}*)NativePtr)->{1}",
@class.QualifiedOriginalName, decl.OriginalName); @class.QualifiedOriginalName, decl.OriginalName);
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
Declaration = decl, Declaration = decl,
ArgName = decl.Name, ArgName = decl.Name,
@ -501,7 +501,7 @@ namespace CppSharp.Generators.CLI
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
} }
@ -524,12 +524,12 @@ namespace CppSharp.Generators.CLI
{ {
WriteLine("void {0}::{1}::add({2} evt)", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::add({2} evt)", QualifiedIdentifier(@class),
@event.Name, @event.Type); @event.Name, @event.Type);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var delegateName = string.Format("_{0}Delegate", @event.Name); var delegateName = string.Format("_{0}Delegate", @event.Name);
WriteLine("if (!{0}Instance)", delegateName); WriteLine("if (!{0}Instance)", delegateName);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var typePrinter = new CppTypePrinter(); var typePrinter = new CppTypePrinter();
var args = typePrinter.VisitParameters(@event.Parameters, hasNames: false); var args = typePrinter.VisitParameters(@event.Parameters, hasNames: false);
@ -543,24 +543,24 @@ namespace CppSharp.Generators.CLI
WriteLine("((::{0}*)NativePtr)->{1}.Connect(_fptr);", @class.QualifiedOriginalName, WriteLine("((::{0}*)NativePtr)->{1}.Connect(_fptr);", @class.QualifiedOriginalName,
@event.OriginalName); @event.OriginalName);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteLine("_{0} = static_cast<{1}>(System::Delegate::Combine(_{0}, evt));", WriteLine("_{0} = static_cast<{1}>(System::Delegate::Combine(_{0}, evt));",
@event.Name, @event.Type); @event.Name, @event.Type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateEventRemove(Event @event, Class @class) private void GenerateEventRemove(Event @event, Class @class)
{ {
WriteLine("void {0}::{1}::remove({2} evt)", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::remove({2} evt)", QualifiedIdentifier(@class),
@event.Name, @event.Type); @event.Name, @event.Type);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("_{0} = static_cast<{1}>(System::Delegate::Remove(_{0}, evt));", WriteLine("_{0} = static_cast<{1}>(System::Delegate::Remove(_{0}, evt));",
@event.Name, @event.Type); @event.Name, @event.Type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateEventRaise(Event @event, Class @class) private void GenerateEventRaise(Event @event, Class @class)
@ -571,12 +571,12 @@ namespace CppSharp.Generators.CLI
WriteLine("void {0}::{1}::raise({2})", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::raise({2})", QualifiedIdentifier(@class),
@event.Name, args); @event.Name, args);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var paramNames = @event.Parameters.Select(param => param.Name).ToList(); var paramNames = @event.Parameters.Select(param => param.Name).ToList();
WriteLine("_{0}({1});", @event.Name, string.Join(", ", paramNames)); WriteLine("_{0}({1});", @event.Name, string.Join(", ", paramNames));
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateEventRaiseWrapper(Event @event, Class @class) private void GenerateEventRaiseWrapper(Event @event, Class @class)
@ -587,12 +587,12 @@ namespace CppSharp.Generators.CLI
WriteLine("void {0}::_{1}Raise({2})", QualifiedIdentifier(@class), WriteLine("void {0}::_{1}Raise({2})", QualifiedIdentifier(@class),
@event.Name, args); @event.Name, args);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var returns = new List<string>(); var returns = new List<string>();
foreach (var param in @event.Parameters) foreach (var param in @event.Parameters)
{ {
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
ReturnVarName = param.Name, ReturnVarName = param.Name,
ReturnType = param.QualifiedType ReturnType = param.QualifiedType
@ -608,7 +608,7 @@ namespace CppSharp.Generators.CLI
Write("{0}", string.Join(", ", returns)); Write("{0}", string.Join(", ", returns));
WriteLine(");"); WriteLine(");");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateVariable(Variable variable, Class @class) private void GenerateVariable(Variable variable, Class @class)
@ -634,14 +634,14 @@ namespace CppSharp.Generators.CLI
if (CLIGenerator.ShouldGenerateClassNativeField(@class)) if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{ {
PushIndent(); Indent();
Write(hasBase ? "," : ":"); Write(hasBase ? "," : ":");
PopIndent(); Unindent();
WriteLine(" {0}(false)", Helpers.OwnsNativeInstanceIdentifier); WriteLine(" {0}(false)", Helpers.OwnsNativeInstanceIdentifier);
} }
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
const string nativePtr = "native"; const string nativePtr = "native";
@ -657,15 +657,15 @@ namespace CppSharp.Generators.CLI
GenerateStructMarshaling(@class, nativePtr + "->"); GenerateStructMarshaling(@class, nativePtr + "->");
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier); WriteLine("{0}^ {0}::{1}(::System::IntPtr native)", qualifiedIdentifier, Helpers.CreateInstanceIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType); WriteLine("return gcnew ::{0}(({1}) native.ToPointer());", qualifiedIdentifier, nativeType);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
} }
@ -686,7 +686,7 @@ namespace CppSharp.Generators.CLI
var nativeField = string.Format("{0}{1}", var nativeField = string.Format("{0}{1}",
nativeVar, property.Field.OriginalName); nativeVar, property.Field.OriginalName);
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
ArgName = property.Name, ArgName = property.Name,
ReturnVarName = nativeField, ReturnVarName = nativeField,
@ -713,7 +713,7 @@ namespace CppSharp.Generators.CLI
if (!@class.IsValueType) if (!@class.IsValueType)
{ {
PushIndent(); Indent();
var baseClass = @class.Bases[0].Class; var baseClass = @class.Bases[0].Class;
Write(": {0}(", QualifiedIdentifier(baseClass)); Write(": {0}(", QualifiedIdentifier(baseClass));
@ -726,7 +726,7 @@ namespace CppSharp.Generators.CLI
WriteLine("{0})", method != null ? "nullptr" : "native"); WriteLine("{0})", method != null ? "nullptr" : "native");
PopIndent(); Unindent();
} }
return true; return true;
@ -753,7 +753,7 @@ namespace CppSharp.Generators.CLI
if (method.IsConstructor) if (method.IsConstructor)
GenerateClassConstructorBase(@class, method: method); GenerateClassConstructorBase(@class, method: method);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
PushBlock(BlockKind.MethodBody, method); PushBlock(BlockKind.MethodBody, method);
@ -792,7 +792,7 @@ namespace CppSharp.Generators.CLI
PopBlock(); PopBlock();
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
if (method.OperatorKind == CXXOperatorKind.EqualEqual) if (method.OperatorKind == CXXOperatorKind.EqualEqual)
{ {
@ -814,7 +814,7 @@ namespace CppSharp.Generators.CLI
NewLine(); NewLine();
var qualifiedIdentifier = QualifiedIdentifier(@class); var qualifiedIdentifier = QualifiedIdentifier(@class);
WriteLine("bool {0}::Equals(::System::Object^ obj)", qualifiedIdentifier); WriteLine("bool {0}::Equals(::System::Object^ obj)", qualifiedIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
WriteLine("return this == safe_cast<{0}^>(obj);", qualifiedIdentifier); WriteLine("return this == safe_cast<{0}^>(obj);", qualifiedIdentifier);
@ -823,7 +823,7 @@ namespace CppSharp.Generators.CLI
{ {
WriteLine("return *this == safe_cast<{0}>(obj);", qualifiedIdentifier); WriteLine("return *this == safe_cast<{0}>(obj);", qualifiedIdentifier);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
} }
@ -834,7 +834,7 @@ namespace CppSharp.Generators.CLI
var paramIndex = 0; var paramIndex = 0;
foreach (var param in method.Parameters) foreach (var param in method.Parameters)
{ {
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
Function = method, Function = method,
Parameter = param, Parameter = param,
@ -870,7 +870,7 @@ namespace CppSharp.Generators.CLI
var varName = string.Format("_native.{0}", property.Field.OriginalName); var varName = string.Format("_native.{0}", property.Field.OriginalName);
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
ReturnVarName = varName, ReturnVarName = varName,
ReturnType = property.QualifiedType ReturnType = property.QualifiedType
@ -908,11 +908,11 @@ namespace CppSharp.Generators.CLI
} }
WriteLine(")"); WriteLine(")");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
GenerateFunctionCall(function); GenerateFunctionCall(function);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
public void GenerateFunctionCall(Function function, Class @class = null, Type publicRetType = null) public void GenerateFunctionCall(Function function, Class @class = null, Type publicRetType = null)
@ -946,7 +946,7 @@ namespace CppSharp.Generators.CLI
WriteLine("auto {0} = ::{1}();", valueMarshalName, @class.QualifiedOriginalName); WriteLine("auto {0} = ::{1}();", valueMarshalName, @class.QualifiedOriginalName);
var param = new Parameter { Name = "(*this)" , Namespace = function.Namespace }; var param = new Parameter { Name = "(*this)" , Namespace = function.Namespace };
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
MarshalVarPrefix = valueMarshalName, MarshalVarPrefix = valueMarshalName,
Parameter = param Parameter = param
@ -1021,7 +1021,7 @@ namespace CppSharp.Generators.CLI
var nativeVarName = paramInfo.Name; var nativeVarName = paramInfo.Name;
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
ArgName = nativeVarName, ArgName = nativeVarName,
ReturnVarName = nativeVarName, ReturnVarName = nativeVarName,
@ -1054,7 +1054,7 @@ namespace CppSharp.Generators.CLI
isIntPtr ? "System::IntPtr()" : "nullptr"); isIntPtr ? "System::IntPtr()" : "nullptr");
} }
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
ArgName = returnIdentifier, ArgName = returnIdentifier,
ReturnVarName = returnIdentifier, ReturnVarName = returnIdentifier,
@ -1167,7 +1167,7 @@ namespace CppSharp.Generators.CLI
QualifiedType = new QualifiedType(paramType) QualifiedType = new QualifiedType(paramType)
}; };
var ctx = new MarshalContext(Context, CurrentIndent) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
Parameter = effectiveParam, Parameter = effectiveParam,
ParameterIndex = paramIndex, ParameterIndex = paramIndex,

36
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -11,13 +11,13 @@ namespace CppSharp.Generators.CSharp
{ {
public class CSharpMarshalContext : MarshalContext public class CSharpMarshalContext : MarshalContext
{ {
public CSharpMarshalContext(BindingContext context, Stack<uint> indent) public CSharpMarshalContext(BindingContext context, Stack<uint> indentation)
: base(context, indent) : base(context, indentation)
{ {
ArgumentPrefix = new TextGenerator(); ArgumentPrefix = new TextGenerator();
indent.PushTo(ArgumentPrefix.CurrentIndent); indentation.PushTo(ArgumentPrefix.CurrentIndentation);
Cleanup = new TextGenerator(); Cleanup = new TextGenerator();
indent.PushTo(Cleanup.CurrentIndent); indentation.PushTo(Cleanup.CurrentIndentation);
} }
public TextGenerator ArgumentPrefix { get; private set; } public TextGenerator ArgumentPrefix { get; private set; }
@ -94,7 +94,7 @@ namespace CppSharp.Generators.CSharp
var arrayType = array.Type.Desugar(); var arrayType = array.Type.Desugar();
supportBefore.WriteLine($"{arrayType}[] {value} = null;"); supportBefore.WriteLine($"{arrayType}[] {value} = null;");
supportBefore.WriteLine($"if ({Context.ReturnVarName} != null)"); supportBefore.WriteLine($"if ({Context.ReturnVarName} != null)");
supportBefore.WriteStartBraceIndent(); supportBefore.WriteOpenBraceAndIndent();
supportBefore.WriteLine($"{value} = new {arrayType}[{array.Size}];"); supportBefore.WriteLine($"{value} = new {arrayType}[{array.Size}];");
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)"); supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void)) if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
@ -130,7 +130,7 @@ namespace CppSharp.Generators.CSharp
Context.ReturnVarName}[i];"); Context.ReturnVarName}[i];");
} }
} }
supportBefore.WriteCloseBraceIndent(); supportBefore.UnindentAndWriteCloseBrace();
Context.Return.Write(value); Context.Return.Write(value);
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
@ -338,7 +338,7 @@ namespace CppSharp.Generators.CSharp
if (parameter.Usage == ParameterUsage.Unknown || parameter.IsIn) if (parameter.Usage == ParameterUsage.Unknown || parameter.IsIn)
return base.VisitParameterDecl(parameter); return base.VisitParameterDecl(parameter);
var ctx = new CSharpMarshalContext(Context.Context, Context.Indent) var ctx = new CSharpMarshalContext(Context.Context, Context.Indentation)
{ {
ReturnType = Context.ReturnType, ReturnType = Context.ReturnType,
ReturnVarName = Context.ReturnVarName ReturnVarName = Context.ReturnVarName
@ -431,28 +431,28 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLineIndent($"{intermediateArray} = null;"); Context.Before.WriteLineIndent($"{intermediateArray} = null;");
Context.Before.WriteLine("else"); Context.Before.WriteLine("else");
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
Context.Before.WriteLine($@"{intermediateArray} = new { Context.Before.WriteLine($@"{intermediateArray} = new {
intermediateArrayType}[{Context.ReturnVarName}.Length];"); intermediateArrayType}[{Context.ReturnVarName}.Length];");
Context.Before.WriteLine($"for (int i = 0; i < {intermediateArray}.Length; i++)"); Context.Before.WriteLine($"for (int i = 0; i < {intermediateArray}.Length; i++)");
if (arrayType.IsAddress()) if (arrayType.IsAddress())
{ {
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
string element = Generator.GeneratedIdentifier("element"); string element = Generator.GeneratedIdentifier("element");
Context.Before.WriteLine($"var {element} = {Context.ReturnVarName}[i];"); Context.Before.WriteLine($"var {element} = {Context.ReturnVarName}[i];");
var intPtrZero = $"{CSharpTypePrinter.IntPtrType}.Zero"; var intPtrZero = $"{CSharpTypePrinter.IntPtrType}.Zero";
Context.Before.WriteLine($@"{intermediateArray}[i] = {element} == { Context.Before.WriteLine($@"{intermediateArray}[i] = {element} == {
intPtrZero} ? null : {intermediateArrayType}.{ intPtrZero} ? null : {intermediateArrayType}.{
Helpers.CreateInstanceIdentifier}({element});"); Helpers.CreateInstanceIdentifier}({element});");
Context.Before.WriteCloseBraceIndent(); Context.Before.UnindentAndWriteCloseBrace();
} }
else else
Context.Before.WriteLineIndent($@"{intermediateArray}[i] = { Context.Before.WriteLineIndent($@"{intermediateArray}[i] = {
intermediateArrayType}.{Helpers.CreateInstanceIdentifier}({ intermediateArrayType}.{Helpers.CreateInstanceIdentifier}({
Context.ReturnVarName}[i]);"); Context.ReturnVarName}[i]);");
Context.Before.WriteCloseBraceIndent(); Context.Before.UnindentAndWriteCloseBrace();
Context.Return.Write(intermediateArray); Context.Return.Write(intermediateArray);
} }
@ -507,7 +507,7 @@ namespace CppSharp.Generators.CSharp
var supportBefore = Context.Before; var supportBefore = Context.Before;
supportBefore.WriteLine($"if ({Context.ArgName} != null)"); supportBefore.WriteLine($"if ({Context.ArgName} != null)");
supportBefore.WriteStartBraceIndent(); supportBefore.WriteOpenBraceAndIndent();
Class @class; Class @class;
var arrayType = array.Type.Desugar(); var arrayType = array.Type.Desugar();
var finalArrayType = arrayType.GetPointee() ?? arrayType; var finalArrayType = arrayType.GetPointee() ?? arrayType;
@ -546,7 +546,7 @@ namespace CppSharp.Generators.CSharp
(arrayType.IsPointerToPrimitiveType(PrimitiveType.Void) ? (arrayType.IsPointerToPrimitiveType(PrimitiveType.Void) ?
".ToPointer()" : string.Empty)};"); ".ToPointer()" : string.Empty)};");
} }
supportBefore.WriteCloseBraceIndent(); supportBefore.UnindentAndWriteCloseBrace();
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
MarshalArray(array); MarshalArray(array);
@ -603,7 +603,7 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLine( Context.Before.WriteLine(
$"fixed ({realPointer} {refParamPtr} = &{Context.Parameter.Name})"); $"fixed ({realPointer} {refParamPtr} = &{Context.Parameter.Name})");
Context.HasCodeBlock = true; Context.HasCodeBlock = true;
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
Context.Return.Write(refParamPtr); Context.Return.Write(refParamPtr);
} }
return true; return true;
@ -961,12 +961,12 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLineIndent($"{intermediateArray} = null;"); Context.Before.WriteLineIndent($"{intermediateArray} = null;");
Context.Before.WriteLine("else"); Context.Before.WriteLine("else");
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
Context.Before.WriteLine($@"{intermediateArray} = new { Context.Before.WriteLine($@"{intermediateArray} = new {
intermediateArrayType}[{Context.Parameter.Name}.Length];"); intermediateArrayType}[{Context.Parameter.Name}.Length];");
Context.Before.WriteLine($"for (int i = 0; i < {intermediateArray}.Length; i++)"); Context.Before.WriteLine($"for (int i = 0; i < {intermediateArray}.Length; i++)");
Context.Before.WriteStartBraceIndent(); Context.Before.WriteOpenBraceAndIndent();
string element = Generator.GeneratedIdentifier("element"); string element = Generator.GeneratedIdentifier("element");
Context.Before.WriteLine($"var {element} = {Context.Parameter.Name}[i];"); Context.Before.WriteLine($"var {element} = {Context.Parameter.Name}[i];");
if (elementType.IsAddress()) if (elementType.IsAddress())
@ -979,9 +979,9 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({ Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({
element}, null) ? new {intermediateArrayType}() : *({ element}, null) ? new {intermediateArrayType}() : *({
intermediateArrayType}*) {element}.{Helpers.InstanceIdentifier};"); intermediateArrayType}*) {element}.{Helpers.InstanceIdentifier};");
Context.Before.WriteCloseBraceIndent(); Context.Before.UnindentAndWriteCloseBrace();
Context.Before.WriteCloseBraceIndent(); Context.Before.UnindentAndWriteCloseBrace();
Context.Return.Write(intermediateArray); Context.Return.Write(intermediateArray);
} }

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

@ -71,7 +71,7 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(BlockKind.Namespace); PushBlock(BlockKind.Namespace);
WriteLine("namespace {0}", Module.OutputNamespace); WriteLine("namespace {0}", Module.OutputNamespace);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
foreach (var unit in TranslationUnits) foreach (var unit in TranslationUnits)
@ -79,7 +79,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrEmpty(Module.OutputNamespace)) if (!string.IsNullOrEmpty(Module.OutputNamespace))
{ {
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -97,7 +97,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrEmpty(group.Key)) if (!string.IsNullOrEmpty(group.Key))
{ {
WriteLine($"namespace {group.Key}"); WriteLine($"namespace {group.Key}");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
foreach (var template in from s in @group foreach (var template in from s in @group
@ -115,18 +115,18 @@ namespace CppSharp.Generators.CSharp
foreach (var declarationContext in declarationContexts) foreach (var declarationContext in declarationContexts)
{ {
WriteLine($"namespace {declarationContext.Name}"); WriteLine($"namespace {declarationContext.Name}");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
GenerateClassTemplateSpecializationsInternals( GenerateClassTemplateSpecializationsInternals(
template.Key, template.ToList()); template.Key, template.ToList());
foreach (var declarationContext in declarationContexts) foreach (var declarationContext in declarationContexts)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
if (!string.IsNullOrEmpty(group.Key)) if (!string.IsNullOrEmpty(group.Key))
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
} }
@ -167,14 +167,14 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(BlockKind.Namespace); PushBlock(BlockKind.Namespace);
WriteLine("namespace {0}", context.Name); WriteLine("namespace {0}", context.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
var ret = base.VisitNamespace(@namespace); var ret = base.VisitNamespace(@namespace);
if (shouldGenerateNamespace) if (shouldGenerateNamespace)
{ {
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -240,11 +240,11 @@ namespace CppSharp.Generators.CSharp
if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any()) if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any())
keyword = "struct"; keyword = "struct";
WriteLine($"public unsafe partial {keyword} {parentName}"); WriteLine($"public unsafe partial {keyword} {parentName}");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
PushBlock(BlockKind.InternalsClass); PushBlock(BlockKind.InternalsClass);
GenerateClassInternalHead(); GenerateClassInternalHead();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
// Generate all the internal function declarations. // Generate all the internal function declarations.
foreach (var function in context.Functions) foreach (var function in context.Functions)
@ -255,7 +255,7 @@ namespace CppSharp.Generators.CSharp
GenerateInternalFunction(function); GenerateInternalFunction(function);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
foreach (var function in context.Functions) foreach (var function in context.Functions)
@ -269,7 +269,7 @@ namespace CppSharp.Generators.CSharp
v => v.IsGenerated && v.Access == AccessSpecifier.Public)) v => v.IsGenerated && v.Access == AccessSpecifier.Public))
GenerateVariable(null, variable); GenerateVariable(null, variable);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -292,7 +292,7 @@ namespace CppSharp.Generators.CSharp
!classTemplate.OriginalNamespace.IsDependent ? !classTemplate.OriginalNamespace.IsDependent ?
classTemplate.OriginalNamespace.Name + '_' : string.Empty, classTemplate.OriginalNamespace.Name + '_' : string.Empty,
classTemplate.Name); classTemplate.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
foreach (var nestedTemplate in classTemplate.Classes.Where( foreach (var nestedTemplate in classTemplate.Classes.Where(
c => c.IsDependent && !c.Ignore && c.Specializations.Any(s => !s.Ignore))) c => c.IsDependent && !c.Ignore && c.Specializations.Any(s => !s.Ignore)))
@ -310,21 +310,21 @@ namespace CppSharp.Generators.CSharp
GenerateNestedInternals(group.Key, GetGeneratedClasses(nested, group)); GenerateNestedInternals(group.Key, GetGeneratedClasses(nested, group));
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateNestedInternals(string name, IEnumerable<Class> nestedClasses) private void GenerateNestedInternals(string name, IEnumerable<Class> nestedClasses)
{ {
WriteLine($"namespace {name}"); WriteLine($"namespace {name}");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
foreach (var nestedClass in nestedClasses) foreach (var nestedClass in nestedClasses)
{ {
GenerateClassInternals(nestedClass); GenerateClassInternals(nestedClass);
foreach (var nestedInNested in nestedClass.Classes) foreach (var nestedInNested in nestedClass.Classes)
GenerateNestedInternals(nestedInNested.Name, new[] { nestedInNested }); GenerateNestedInternals(nestedInNested.Name, new[] { nestedInNested });
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
} }
@ -388,7 +388,7 @@ namespace CppSharp.Generators.CSharp
GenerateClassSpecifier(@class); GenerateClassSpecifier(@class);
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (!@class.IsAbstractImpl && !@class.IsDependent) if (!@class.IsAbstractImpl && !@class.IsDependent)
GenerateClassInternals(@class); GenerateClassInternals(@class);
@ -440,7 +440,7 @@ namespace CppSharp.Generators.CSharp
if (@class.IsDynamic) if (@class.IsDynamic)
GenerateVTable(@class); GenerateVTable(@class);
exit: exit:
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
for (int i = 0; i < typeMaps.Count; i++) for (int i = 0; i < typeMaps.Count; i++)
@ -463,7 +463,7 @@ namespace CppSharp.Generators.CSharp
Write(" : IDisposable"); Write(" : IDisposable");
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
foreach (var method in @class.Methods.Where(m => foreach (var method in @class.Methods.Where(m =>
(m.OriginalFunction == null || (m.OriginalFunction == null ||
@ -505,7 +505,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -518,7 +518,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($"[StructLayout(LayoutKind.Sequential, Pack = {@class.MaxFieldAlignment})]"); WriteLine($"[StructLayout(LayoutKind.Sequential, Pack = {@class.MaxFieldAlignment})]");
GenerateClassInternalHead(@class); GenerateClassInternalHead(@class);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
@ -534,7 +534,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext(); TypePrinter.PopContext();
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -763,14 +763,14 @@ namespace CppSharp.Generators.CSharp
} }
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true); this.GenerateMember(@class, c => GenerateFunctionSetter(c, property), true);
} }
else if (decl is Variable) else if (decl is Variable)
{ {
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
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 ?
@ -784,11 +784,11 @@ namespace CppSharp.Generators.CSharp
return; return;
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
this.GenerateField(@class, field, GenerateFieldSetter, true); this.GenerateField(@class, field, GenerateFieldSetter, true);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -820,7 +820,7 @@ namespace CppSharp.Generators.CSharp
QualifiedType = var.QualifiedType QualifiedType = var.QualifiedType
}; };
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
@ -835,12 +835,12 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
PushIndent(); Indent();
WriteLine($"*{ptr} = {marshal.Context.Return};", marshal.Context.Return); WriteLine($"*{ptr} = {marshal.Context.Return};", marshal.Context.Return);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateFunctionSetter(Class @class, Property property) private void GenerateFunctionSetter(Class @class, Property property)
@ -903,7 +903,7 @@ namespace CppSharp.Generators.CSharp
QualifiedType = field.QualifiedType QualifiedType = field.QualifiedType
}; };
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
@ -920,7 +920,7 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
PushIndent(); Indent();
if (marshal.Context.Return.StringBuilder.Length > 0) if (marshal.Context.Return.StringBuilder.Length > 0)
{ {
@ -940,7 +940,7 @@ namespace CppSharp.Generators.CSharp
} }
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock) if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private string HandleValueArray(ArrayType arrayType, Field field) private string HandleValueArray(ArrayType arrayType, Field field)
@ -960,7 +960,7 @@ namespace CppSharp.Generators.CSharp
f => f.FieldPtr == field.OriginalPtr).Name; f => f.FieldPtr == field.OriginalPtr).Name;
WriteLine(string.Format("fixed ({0} {1} = {2}.{3})", WriteLine(string.Format("fixed ({0} {1} = {2}.{3})",
type, arrPtr, Helpers.InstanceField, SafeIdentifier(name))); type, arrPtr, Helpers.InstanceField, SafeIdentifier(name)));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
return arrPtr; return arrPtr;
} }
@ -971,13 +971,13 @@ namespace CppSharp.Generators.CSharp
return false; return false;
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0} = value;", name); WriteLine("{0} = value;", name);
WriteLine("if (!{0}{1})", name, "Initialised"); WriteLine("if (!{0}{1})", name, "Initialised");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0}{1} = true;", name, "Initialised"); WriteLine("{0}{1} = true;", name, "Initialised");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return true; return true;
@ -989,7 +989,7 @@ namespace CppSharp.Generators.CSharp
function.OriginalReturnType.Type.IsPointerTo(out type); function.OriginalReturnType.Type.IsPointerTo(out type);
var @internal = TypePrinter.PrintNative(function.Namespace); var @internal = TypePrinter.PrintNative(function.Namespace);
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
Parameter = new Parameter Parameter = new Parameter
{ {
@ -1033,11 +1033,11 @@ namespace CppSharp.Generators.CSharp
property.GetMethod.SynthKind == FunctionSynthKind.InterfaceInstance) property.GetMethod.SynthKind == FunctionSynthKind.InterfaceInstance)
{ {
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var to = ((Class) property.OriginalNamespace).OriginalClass; var to = ((Class) property.OriginalNamespace).OriginalClass;
var baseOffset = GetOffsetToBase(@class, to); var baseOffset = GetOffsetToBase(@class, to);
WriteLine("return {0} + {1};", Helpers.InstanceIdentifier, baseOffset); WriteLine("return {0} + {1};", Helpers.InstanceIdentifier, baseOffset);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return; return;
} }
@ -1052,7 +1052,7 @@ namespace CppSharp.Generators.CSharp
} }
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
this.GenerateMember(@class, c => GenerateFunctionGetter(c, property)); this.GenerateMember(@class, c => GenerateFunctionGetter(c, property));
} }
@ -1063,19 +1063,19 @@ namespace CppSharp.Generators.CSharp
return; return;
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
this.GenerateField(@class, field, GenerateFieldGetter, false); this.GenerateField(@class, field, GenerateFieldGetter, false);
} }
else if (decl is Variable) else if (decl is Variable)
{ {
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var var = decl as Variable; var var = decl as Variable;
this.GenerateMember(@class, c => GenerateVariableGetter( this.GenerateMember(@class, c => GenerateVariableGetter(
c is ClassTemplateSpecialization ? c is ClassTemplateSpecialization ?
c.Variables.First(v => v.Name == decl.Name) : var)); c.Variables.First(v => v.Name == decl.Name) : var));
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -1110,7 +1110,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext(); TypePrinter.PopContext();
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ArgName = var.Name, ArgName = var.Name,
ReturnType = new QualifiedType(var.Type) ReturnType = new QualifiedType(var.Type)
@ -1134,12 +1134,12 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
PushIndent(); Indent();
WriteLine("return {0};", marshal.Context.Return); WriteLine("return {0};", marshal.Context.Return);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateFunctionGetter(Class @class, Property property) private void GenerateFunctionGetter(Class @class, Property property)
@ -1193,7 +1193,7 @@ namespace CppSharp.Generators.CSharp
returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})"; returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})";
} }
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ArgName = field.Name, ArgName = field.Name,
Declaration = field, Declaration = field,
@ -1209,7 +1209,7 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
PushIndent(); Indent();
Write("return "); Write("return ");
@ -1233,7 +1233,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($"{@return};"); WriteLine($"{@return};");
if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock) if ((arrayType != null && @class.IsValueType) || ctx.HasCodeBlock)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private bool WrapGetterArrayOfPointers(string name, Type fieldType) private bool WrapGetterArrayOfPointers(string name, Type fieldType)
@ -1242,14 +1242,14 @@ namespace CppSharp.Generators.CSharp
if (arrayType != null && arrayType.Type.IsPointerToPrimitiveType()) if (arrayType != null && arrayType.Type.IsPointerToPrimitiveType())
{ {
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("if (!{0}{1})", name, "Initialised"); WriteLine("if (!{0}{1})", name, "Initialised");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0} = null;", name); WriteLine("{0} = null;", name);
WriteLine("{0}{1} = true;", name, "Initialised"); WriteLine("{0}{1} = true;", name, "Initialised");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteLine("return {0};", name); WriteLine("return {0};", name);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return true; return true;
} }
@ -1368,7 +1368,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($@"{printedType} { WriteLine($@"{printedType} {
prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}"); prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}");
} }
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (prop.Field != null) if (prop.Field != null)
{ {
@ -1387,7 +1387,7 @@ namespace CppSharp.Generators.CSharp
GeneratePropertySetter(prop.SetMethod, @class, prop.IsPure, prop); GeneratePropertySetter(prop.SetMethod, @class, prop.IsPure, prop);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
} }
@ -1416,7 +1416,7 @@ namespace CppSharp.Generators.CSharp
var variableType = variable.Type.Visit(TypePrinter); var variableType = variable.Type.Visit(TypePrinter);
TypePrinter.PopMarshalKind(); TypePrinter.PopMarshalKind();
WriteLine($"public static {variableType} {variable.Name}"); WriteLine($"public static {variableType} {variable.Name}");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
GeneratePropertyGetter(variable, @class); GeneratePropertyGetter(variable, @class);
@ -1424,7 +1424,7 @@ namespace CppSharp.Generators.CSharp
!(variable.Type.Desugar() is ArrayType)) !(variable.Type.Desugar() is ArrayType))
GeneratePropertySetter(variable, @class); GeneratePropertySetter(variable, @class);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -1474,7 +1474,7 @@ namespace CppSharp.Generators.CSharp
{ {
const string destructorOnly = "destructorOnly"; const string destructorOnly = "destructorOnly";
WriteLine("private void SetupVTables(bool {0} = false)", destructorOnly); WriteLine("private void SetupVTables(bool {0} = false)", destructorOnly);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("if (__OriginalVTables != null)"); WriteLine("if (__OriginalVTables != null)");
WriteLineIndent("return;"); WriteLineIndent("return;");
@ -1492,7 +1492,7 @@ namespace CppSharp.Generators.CSharp
// Get the _Thunks // Get the _Thunks
WriteLine("if (_Thunks == null)"); WriteLine("if (_Thunks == null)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("_Thunks = new void*[{0}];", wrappedEntries.Count); WriteLine("_Thunks = new void*[{0}];", wrappedEntries.Count);
var uniqueEntries = new HashSet<VTableComponent>(); var uniqueEntries = new HashSet<VTableComponent>();
@ -1508,32 +1508,32 @@ namespace CppSharp.Generators.CSharp
WriteLine("_Thunks[{0}] = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();", WriteLine("_Thunks[{0}] = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();",
i, instance); i, instance);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
if (hasVirtualDtor) if (hasVirtualDtor)
{ {
WriteLine("if ({0})", destructorOnly); WriteLine("if ({0})", destructorOnly);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("if (__ManagedVTablesDtorOnly == null)"); WriteLine("if (__ManagedVTablesDtorOnly == null)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
AllocateNewVTables(@class, wrappedEntries, true); AllocateNewVTables(@class, wrappedEntries, true);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteLine("else"); WriteLine("else");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
WriteLine("if (__ManagedVTables == null)"); WriteLine("if (__ManagedVTables == null)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
AllocateNewVTables(@class, wrappedEntries, false); AllocateNewVTables(@class, wrappedEntries, false);
if (hasVirtualDtor) if (hasVirtualDtor)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
} }
@ -1577,7 +1577,7 @@ namespace CppSharp.Generators.CSharp
@class.Layout.VTablePointers[i].Offset, i, destructorOnly); @class.Layout.VTablePointers[i].Offset, i, destructorOnly);
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
for (int i = 0; i < @class.Layout.VTablePointers.Count; i++) for (int i = 0; i < @class.Layout.VTablePointers.Count; i++)
@ -1603,7 +1603,7 @@ namespace CppSharp.Generators.CSharp
AllocateNewVTableEntries(@class.Layout.Layout.Components, AllocateNewVTableEntries(@class.Layout.Layout.Components,
wrappedEntries, @class.Layout.VTablePointers[0].Offset, 0, destructorOnly); wrappedEntries, @class.Layout.VTablePointers[0].Offset, 0, destructorOnly);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
var offset = @class.Layout.VTablePointers[0].Offset; var offset = @class.Layout.VTablePointers[0].Offset;
@ -1669,7 +1669,7 @@ namespace CppSharp.Generators.CSharp
if (param.Kind == ParameterKind.IndirectReturnType) if (param.Kind == ParameterKind.IndirectReturnType)
continue; continue;
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ReturnType = param.QualifiedType, ReturnType = param.QualifiedType,
ReturnVarName = param.Name, ReturnVarName = param.Name,
@ -1687,7 +1687,7 @@ namespace CppSharp.Generators.CSharp
marshals.Add(marshal.Context.Return); marshals.Add(marshal.Context.Return);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
{ {
PushIndent(); Indent();
numBlocks++; numBlocks++;
} }
} }
@ -1726,7 +1726,7 @@ namespace CppSharp.Generators.CSharp
}; };
// Marshal the managed result to native // Marshal the managed result to native
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ArgName = Helpers.ReturnIdentifier, ArgName = Helpers.ReturnIdentifier,
Parameter = param, Parameter = param,
@ -1757,7 +1757,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("return false;"); WriteLine("return false;");
for (var i = 0; i < numBlocks; ++i) for (var i = 0; i < numBlocks; ++i)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateVTableMethodDelegates(Class @class, Method method) private void GenerateVTableMethodDelegates(Class @class, Method method)
@ -1787,7 +1787,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("private static {0} {1}Hook({2})", retType, vTableMethodDelegateName, WriteLine("private static {0} {1}Hook({2})", retType, vTableMethodDelegateName,
string.Join(", ", @params)); string.Join(", ", @params));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("if (!NativeToManagedMap.ContainsKey(instance))"); WriteLine("if (!NativeToManagedMap.ContainsKey(instance))");
WriteLineIndent("throw new global::System.Exception(\"No managed instance was found\");"); WriteLineIndent("throw new global::System.Exception(\"No managed instance was found\");");
@ -1799,7 +1799,7 @@ namespace CppSharp.Generators.CSharp
WriteLineIndent("{0}.SetupVTables();", Helpers.TargetIdentifier); WriteLineIndent("{0}.SetupVTables();", Helpers.TargetIdentifier);
GenerateVTableManagedCall(method); GenerateVTableManagedCall(method);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.Always); PopBlock(NewLineKind.Always);
} }
@ -1839,14 +1839,14 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} {1};", @event.Type, delegateInstance); WriteLine("{0} {1};", @event.Type, delegateInstance);
WriteLine("public event {0} {1}", @event.Type, @event.Name); WriteLine("public event {0} {1}", @event.Type, @event.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
GenerateEventAdd(@event, delegateRaise, delegateName, delegateInstance); GenerateEventAdd(@event, delegateRaise, delegateName, delegateInstance);
NewLine(); NewLine();
GenerateEventRemove(@event, delegateInstance); GenerateEventRemove(@event, delegateInstance);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
GenerateEventRaiseWrapper(@event, delegateInstance); GenerateEventRaiseWrapper(@event, delegateInstance);
@ -1858,10 +1858,10 @@ namespace CppSharp.Generators.CSharp
private void GenerateEventAdd(Event @event, string delegateRaise, string delegateName, string delegateInstance) private void GenerateEventAdd(Event @event, string delegateRaise, string delegateName, string delegateInstance)
{ {
WriteLine("add"); WriteLine("add");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("if ({0} == null)", delegateRaise); WriteLine("if ({0} == null)", delegateRaise);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name); WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name);
@ -1873,23 +1873,23 @@ namespace CppSharp.Generators.CSharp
//WriteLine("((::{0}*)NativePtr)->{1}.Connect(_fptr);", @class.QualifiedOriginalName, //WriteLine("((::{0}*)NativePtr)->{1}.Connect(_fptr);", @class.QualifiedOriginalName,
// @event.OriginalName); // @event.OriginalName);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteLine("{0} = ({1})System.Delegate.Combine({0}, value);", WriteLine("{0} = ({1})System.Delegate.Combine({0}, value);",
delegateInstance, @event.Type); delegateInstance, @event.Type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateEventRemove(ITypedDecl @event, string delegateInstance) private void GenerateEventRemove(ITypedDecl @event, string delegateInstance)
{ {
WriteLine("remove"); WriteLine("remove");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0} = ({1})System.Delegate.Remove({0}, value);", WriteLine("{0} = ({1})System.Delegate.Remove({0}, value);",
delegateInstance, @event.Type); delegateInstance, @event.Type);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private void GenerateEventRaiseWrapper(Event @event, string delegateInstance) private void GenerateEventRaiseWrapper(Event @event, string delegateInstance)
@ -1899,12 +1899,12 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext(); TypePrinter.PopContext();
WriteLine("void _{0}Raise({1})", @event.Name, args); WriteLine("void _{0}Raise({1})", @event.Name, args);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var returns = new List<string>(); var returns = new List<string>();
foreach (var param in @event.Parameters) foreach (var param in @event.Parameters)
{ {
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ReturnVarName = param.Name, ReturnVarName = param.Name,
ReturnType = param.QualifiedType ReturnType = param.QualifiedType
@ -1917,11 +1917,11 @@ namespace CppSharp.Generators.CSharp
} }
WriteLine("if ({0} != null)", delegateInstance); WriteLine("if ({0} != null)", delegateInstance);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("{0}({1});", delegateInstance, string.Join(", ", returns)); WriteLine("{0}({1});", delegateInstance, string.Join(", ", returns));
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
#endregion #endregion
@ -1969,9 +1969,9 @@ namespace CppSharp.Generators.CSharp
PushBlock(BlockKind.Finalizer); PushBlock(BlockKind.Finalizer);
WriteLine("~{0}()", @class.Name); WriteLine("~{0}()", @class.Name);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("Dispose(false);"); WriteLine("Dispose(false);");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -1985,13 +1985,13 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
WriteLine("public void Dispose()"); WriteLine("public void Dispose()");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
WriteLine("Dispose(disposing: true);"); WriteLine("Dispose(disposing: true);");
if (Options.GenerateFinalizers) if (Options.GenerateFinalizers)
WriteLine("GC.SuppressFinalize(this);"); WriteLine("GC.SuppressFinalize(this);");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2002,7 +2002,7 @@ namespace CppSharp.Generators.CSharp
Write(hasBaseClass ? "override " : "virtual "); Write(hasBaseClass ? "override " : "virtual ");
WriteLine("void Dispose(bool disposing)"); WriteLine("void Dispose(bool disposing)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
@ -2044,9 +2044,9 @@ namespace CppSharp.Generators.CSharp
{ {
WriteLine("if (disposing)"); WriteLine("if (disposing)");
if (@class.IsDependent || dtor.IsVirtual) if (@class.IsDependent || dtor.IsVirtual)
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
else else
PushIndent(); Indent();
if (dtor.IsVirtual) if (dtor.IsVirtual)
this.GenerateMember(@class, c => GenerateDestructorCall( this.GenerateMember(@class, c => GenerateDestructorCall(
c is ClassTemplateSpecialization ? c is ClassTemplateSpecialization ?
@ -2054,9 +2054,9 @@ namespace CppSharp.Generators.CSharp
else else
this.GenerateMember(@class, c => GenerateMethodBody(c, dtor), true); this.GenerateMember(@class, c => GenerateMethodBody(c, dtor), true);
if (@class.IsDependent || dtor.IsVirtual) if (@class.IsDependent || dtor.IsVirtual)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
else else
PopIndent(); Unindent();
} }
} }
@ -2064,7 +2064,7 @@ namespace CppSharp.Generators.CSharp
WriteLineIndent("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier); WriteLineIndent("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier);
WriteLine("{0} = IntPtr.Zero;", Helpers.InstanceIdentifier); WriteLine("{0} = IntPtr.Zero;", Helpers.InstanceIdentifier);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2074,11 +2074,11 @@ namespace CppSharp.Generators.CSharp
GenerateVirtualFunctionCall(dtor, @class, true); GenerateVirtualFunctionCall(dtor, @class, true);
if (@class.IsAbstract) if (@class.IsAbstract)
{ {
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
WriteLine("else"); WriteLine("else");
PushIndent(); Indent();
GenerateInternalFunctionCall(dtor); GenerateInternalFunctionCall(dtor);
PopIndent(); Unindent();
} }
} }
@ -2099,11 +2099,11 @@ namespace CppSharp.Generators.CSharp
WriteLine("internal static {0}{1} {2}(global::System.IntPtr native, bool skipVTables = false)", WriteLine("internal static {0}{1} {2}(global::System.IntPtr native, bool skipVTables = false)",
@class.NeedsBase && !@class.BaseClass.IsInterface ? "new " : string.Empty, @class.NeedsBase && !@class.BaseClass.IsInterface ? "new " : string.Empty,
printedClass, Helpers.CreateInstanceIdentifier); printedClass, Helpers.CreateInstanceIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var suffix = @class.IsAbstract ? "Internal" : string.Empty; var suffix = @class.IsAbstract ? "Internal" : string.Empty;
var ctorCall = $"{printedClass}{suffix}"; var ctorCall = $"{printedClass}{suffix}";
WriteLine("return new {0}(native.ToPointer(), skipVTables);", ctorCall); WriteLine("return new {0}(native.ToPointer(), skipVTables);", ctorCall);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2118,7 +2118,7 @@ namespace CppSharp.Generators.CSharp
if (hasBaseClass) if (hasBaseClass)
WriteLineIndent(": base((void*) null)", @class.BaseClass.Visit(TypePrinter)); WriteLineIndent(": base((void*) null)", @class.BaseClass.Visit(TypePrinter));
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
@ -2138,7 +2138,7 @@ namespace CppSharp.Generators.CSharp
if (setupVTables) if (setupVTables)
{ {
WriteLine("if (skipVTables)"); WriteLine("if (skipVTables)");
PushIndent(); Indent();
} }
if (@class.IsAbstractImpl || hasVTables) if (@class.IsAbstractImpl || hasVTables)
@ -2146,11 +2146,11 @@ namespace CppSharp.Generators.CSharp
if (setupVTables) if (setupVTables)
{ {
PopIndent(); Unindent();
WriteLine("else"); WriteLine("else");
PushIndent(); Indent();
GenerateVTableClassSetupCall(@class, destructorOnly: true); GenerateVTableClassSetupCall(@class, destructorOnly: true);
PopIndent(); Unindent();
} }
} }
else else
@ -2158,7 +2158,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($"{Helpers.InstanceField} = *({TypePrinter.PrintNative(@class)}*) native;"); WriteLine($"{Helpers.InstanceField} = *({TypePrinter.PrintNative(@class)}*) native;");
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2171,10 +2171,10 @@ namespace CppSharp.Generators.CSharp
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
WriteLine("internal static {0} {1}({2} native, bool skipVTables = false)", WriteLine("internal static {0} {1}({2} native, bool skipVTables = false)",
returnType, Helpers.CreateInstanceIdentifier, @internal); returnType, Helpers.CreateInstanceIdentifier, @internal);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var suffix = @class.IsAbstract ? "Internal" : ""; var suffix = @class.IsAbstract ? "Internal" : "";
WriteLine($"return new {returnType}{suffix}(native, skipVTables);"); WriteLine($"return new {returnType}{suffix}(native, skipVTables);");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2182,7 +2182,7 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
WriteLine($"private static void* __CopyValue({@internal} native)"); WriteLine($"private static void* __CopyValue({@internal} native)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var copyCtorMethod = @class.Methods.FirstOrDefault(method => var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
method.IsCopyConstructor); method.IsCopyConstructor);
if (@class.HasNonTrivialCopyConstructor && copyCtorMethod != null && if (@class.HasNonTrivialCopyConstructor && copyCtorMethod != null &&
@ -2201,7 +2201,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($"*({@internal}*) ret = native;"); WriteLine($"*({@internal}*) ret = native;");
WriteLine("return ret.ToPointer();"); WriteLine("return ret.ToPointer();");
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
if (!@class.IsAbstract) if (!@class.IsAbstract)
@ -2210,7 +2210,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} {1}({2} native, bool skipVTables = false)", WriteLine("{0} {1}({2} native, bool skipVTables = false)",
@class.IsAbstractImpl ? "internal" : "private", @class.Name, @internal); @class.IsAbstractImpl ? "internal" : "private", @class.Name, @internal);
WriteLineIndent(@class.IsRefType ? ": this(__CopyValue(native), skipVTables)" : ": this()"); WriteLineIndent(@class.IsRefType ? ": this(__CopyValue(native), skipVTables)" : ": this()");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;"); WriteLine($"{Helpers.OwnsNativeInstanceIdentifier} = true;");
@ -2220,7 +2220,7 @@ namespace CppSharp.Generators.CSharp
{ {
WriteLine($"{Helpers.InstanceField} = native;"); WriteLine($"{Helpers.InstanceField} = native;");
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
} }
@ -2231,13 +2231,13 @@ namespace CppSharp.Generators.CSharp
if (hasBase && !@class.IsValueType) if (hasBase && !@class.IsValueType)
{ {
PushIndent(); Indent();
Write(": this("); Write(": this(");
Write(method != null ? "(void*) null" : "native"); Write(method != null ? "(void*) null" : "native");
WriteLine(")"); WriteLine(")");
PopIndent(); Unindent();
} }
if (@class.IsValueType) if (@class.IsValueType)
@ -2259,14 +2259,14 @@ namespace CppSharp.Generators.CSharp
Write("public static {0} {1}(", function.OriginalReturnType, functionName); Write("public static {0} {1}(", function.OriginalReturnType, functionName);
Write(FormatMethodParameters(function.Parameters)); Write(FormatMethodParameters(function.Parameters));
WriteLine(")"); WriteLine(")");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (function.SynthKind == FunctionSynthKind.DefaultValueOverload) if (function.SynthKind == FunctionSynthKind.DefaultValueOverload)
GenerateOverloadCall(function); GenerateOverloadCall(function);
else else
GenerateInternalFunctionCall(function); GenerateInternalFunctionCall(function);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2342,7 +2342,7 @@ namespace CppSharp.Generators.CSharp
method.SynthKind != FunctionSynthKind.DefaultValueOverload) method.SynthKind != FunctionSynthKind.DefaultValueOverload)
GenerateClassConstructorBase(@class, method); GenerateClassConstructorBase(@class, method);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (method.IsProxy) if (method.IsProxy)
goto SkipImpl; goto SkipImpl;
@ -2369,7 +2369,7 @@ namespace CppSharp.Generators.CSharp
SkipImpl: SkipImpl:
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
if (method.OperatorKind == CXXOperatorKind.EqualEqual) if (method.OperatorKind == CXXOperatorKind.EqualEqual)
{ {
@ -2505,7 +2505,7 @@ namespace CppSharp.Generators.CSharp
{ {
NewLine(); NewLine();
WriteLine("public override bool Equals(object obj)"); WriteLine("public override bool Equals(object obj)");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
var printedClass = @class.Visit(TypePrinter); var printedClass = @class.Visit(TypePrinter);
if (@class.IsRefType) if (@class.IsRefType)
{ {
@ -2516,17 +2516,17 @@ namespace CppSharp.Generators.CSharp
WriteLine($"if (!(obj is {printedClass})) return false;"); WriteLine($"if (!(obj is {printedClass})) return false;");
WriteLine($"return this == ({printedClass}) obj;"); WriteLine($"return this == ({printedClass}) obj;");
} }
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
WriteLine("public override int GetHashCode()"); WriteLine("public override int GetHashCode()");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
this.GenerateMember(@class, GenerateGetHashCode); this.GenerateMember(@class, GenerateGetHashCode);
else else
WriteLine($"return {Helpers.InstanceIdentifier}.GetHashCode();"); WriteLine($"return {Helpers.InstanceIdentifier}.GetHashCode();");
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
} }
@ -2580,7 +2580,7 @@ namespace CppSharp.Generators.CSharp
if (method.IsDestructor && @class.IsAbstract) if (method.IsDestructor && @class.IsAbstract)
{ {
WriteLine("if ({0} != null)", Helpers.SlotIdentifier); WriteLine("if ({0} != null)", Helpers.SlotIdentifier);
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
var @delegate = GetVTableMethodDelegateName(@virtual); var @delegate = GetVTableMethodDelegateName(@virtual);
@ -2810,7 +2810,7 @@ namespace CppSharp.Generators.CSharp
WriteLine($@"fixed ({Helpers.InternalStruct}{ WriteLine($@"fixed ({Helpers.InternalStruct}{
Helpers.GetSuffixForInternal(originalFunction.Namespace)}* __instancePtr = &{ Helpers.GetSuffixForInternal(originalFunction.Namespace)}* __instancePtr = &{
Helpers.InstanceField})"); Helpers.InstanceField})");
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
} }
else else
{ {
@ -2856,7 +2856,7 @@ namespace CppSharp.Generators.CSharp
if (needsReturn) if (needsReturn)
{ {
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
ArgName = Helpers.ReturnIdentifier, ArgName = Helpers.ReturnIdentifier,
ReturnVarName = Helpers.ReturnIdentifier, ReturnVarName = Helpers.ReturnIdentifier,
@ -2872,20 +2872,20 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
PushIndent(); Indent();
WriteLine($"return {marshal.Context.Return};"); WriteLine($"return {marshal.Context.Return};");
if (ctx.HasCodeBlock) if (ctx.HasCodeBlock)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
if (needsFixedThis && operatorParam == null) if (needsFixedThis && operatorParam == null)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
var numFixedBlocks = @params.Count(param => param.HasUsingBlock); var numFixedBlocks = @params.Count(param => param.HasUsingBlock);
for(var i = 0; i < numFixedBlocks; ++i) for(var i = 0; i < numFixedBlocks; ++i)
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
} }
private string GetInstanceParam(Function function) private string GetInstanceParam(Function function)
@ -2947,7 +2947,7 @@ namespace CppSharp.Generators.CSharp
var nativeVarName = paramInfo.Name; var nativeVarName = paramInfo.Name;
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
Parameter = param, Parameter = param,
ArgName = nativeVarName, ArgName = nativeVarName,
@ -3015,7 +3015,7 @@ namespace CppSharp.Generators.CSharp
} }
} }
var ctx = new CSharpMarshalContext(Context, CurrentIndent) var ctx = new CSharpMarshalContext(Context, CurrentIndentation)
{ {
Parameter = param, Parameter = param,
ParameterIndex = paramIndex, ParameterIndex = paramIndex,
@ -3035,7 +3035,7 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.Before); Write(marshal.Context.Before);
if (paramMarshal.HasUsingBlock) if (paramMarshal.HasUsingBlock)
PushIndent(); Indent();
if (marshal.Context.Return.ToString() == param.Name) if (marshal.Context.Return.ToString() == param.Name)
paramMarshal.Name = param.Name; paramMarshal.Name = param.Name;
@ -3120,9 +3120,9 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
WriteStartBraceIndent(); WriteOpenBraceAndIndent();
GenerateEnumItems(@enum); GenerateEnumItems(@enum);
WriteCloseBraceIndent(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);

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

@ -43,13 +43,13 @@ 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.WriteStartBraceIndent(); gen.WriteOpenBraceAndIndent();
var specializedField = specialization.Fields.First( var specializedField = specialization.Fields.First(
f => f.OriginalName == field.OriginalName); f => f.OriginalName == field.OriginalName);
generate(specializedField, specialization, field.QualifiedType); generate(specializedField, specialization, field.QualifiedType);
if (isVoid) if (isVoid)
gen.WriteLine("return;"); gen.WriteLine("return;");
gen.WriteCloseBraceIndent(); gen.UnindentAndWriteCloseBrace();
} }
ThrowException(gen, @class); ThrowException(gen, @class);
} }
@ -79,11 +79,11 @@ 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.WriteStartBraceIndent(); gen.WriteOpenBraceAndIndent();
generate(specialization); generate(specialization);
if (isVoid) if (isVoid)
gen.WriteLine("return;"); gen.WriteLine("return;");
gen.WriteCloseBraceIndent(); gen.UnindentAndWriteCloseBrace();
} }
ThrowException(gen, @class); ThrowException(gen, @class);
} }

10
src/Generator/Generators/Marshal.cs

@ -5,15 +5,15 @@ namespace CppSharp.Generators
{ {
public class MarshalContext : TypePrinter public class MarshalContext : TypePrinter
{ {
public MarshalContext(BindingContext context, Stack<uint> indent) public MarshalContext(BindingContext context, Stack<uint> indentation)
{ {
Context = context; Context = context;
Before = new TextGenerator(); Before = new TextGenerator();
indent.PushTo(Before.CurrentIndent); indentation.PushTo(Before.CurrentIndentation);
Return = new TextGenerator(); Return = new TextGenerator();
indent.PushTo(Return.CurrentIndent); indentation.PushTo(Return.CurrentIndentation);
MarshalVarPrefix = string.Empty; MarshalVarPrefix = string.Empty;
this.Indent = indent; this.Indentation = indentation;
} }
public BindingContext Context { get; } public BindingContext Context { get; }
@ -31,7 +31,7 @@ namespace CppSharp.Generators
public Function Function { get; set; } public Function Function { get; set; }
public string MarshalVarPrefix { get; set; } public string MarshalVarPrefix { get; set; }
public Stack<uint> Indent { get; } public Stack<uint> Indentation { get; }
} }
public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext

12
src/Generator/Types/Std/Stdlib.cs

@ -227,7 +227,7 @@ namespace CppSharp.Types.Std
tmpVarName, nativeType); tmpVarName, nativeType);
ctx.Before.WriteLine("for each({0} _element in {1})", ctx.Before.WriteLine("for each({0} _element in {1})",
managedType, entryString); managedType, entryString);
ctx.Before.WriteStartBraceIndent(); ctx.Before.WriteOpenBraceAndIndent();
{ {
var param = new Parameter var param = new Parameter
{ {
@ -235,7 +235,7 @@ namespace CppSharp.Types.Std
QualifiedType = type QualifiedType = type
}; };
var elementCtx = new MarshalContext(ctx.Context, ctx.Indent) var elementCtx = new MarshalContext(ctx.Context, ctx.Indentation)
{ {
Parameter = param, Parameter = param,
ArgName = param.Name, ArgName = param.Name,
@ -258,7 +258,7 @@ namespace CppSharp.Types.Std
tmpVarName); tmpVarName);
} }
ctx.Before.WriteCloseBraceIndent(); ctx.Before.UnindentAndWriteCloseBrace();
ctx.Return.Write(tmpVarName); ctx.Return.Write(tmpVarName);
} }
@ -278,9 +278,9 @@ namespace CppSharp.Types.Std
tmpVarName, managedType); tmpVarName, managedType);
ctx.Before.WriteLine("for(auto _element : {0})", ctx.Before.WriteLine("for(auto _element : {0})",
ctx.ReturnVarName); ctx.ReturnVarName);
ctx.Before.WriteStartBraceIndent(); ctx.Before.WriteOpenBraceAndIndent();
{ {
var elementCtx = new MarshalContext(ctx.Context, ctx.Indent) var elementCtx = new MarshalContext(ctx.Context, ctx.Indentation)
{ {
ReturnVarName = "_element", ReturnVarName = "_element",
ReturnType = type ReturnType = type
@ -302,7 +302,7 @@ namespace CppSharp.Types.Std
ctx.Before.WriteLine("{0}->Add(_marshalElement);", ctx.Before.WriteLine("{0}->Add(_marshalElement);",
tmpVarName); tmpVarName);
} }
ctx.Before.WriteCloseBraceIndent(); ctx.Before.UnindentAndWriteCloseBrace();
ctx.Return.Write(tmpVarName); ctx.Return.Write(tmpVarName);
} }

36
src/Generator/Utils/BlockGenerator.cs

@ -211,26 +211,26 @@ namespace CppSharp
Text.ResetNewLine(); Text.ResetNewLine();
} }
public void PushIndent(uint indent = 4u) public void Indent(uint indentation = 4u)
{ {
hasIndentChanged = true; hasIndentChanged = true;
Text.PushIndent(indent); Text.Indent(indentation);
} }
public void PopIndent() public void Unindent()
{ {
hasIndentChanged = true; hasIndentChanged = true;
Text.PopIndent(); Text.Unindent();
} }
public void WriteStartBraceIndent() public void WriteOpenBraceAndIndent()
{ {
Text.WriteStartBraceIndent(); Text.WriteOpenBraceAndIndent();
} }
public void WriteCloseBraceIndent() public void UnindentAndWriteCloseBrace()
{ {
Text.WriteCloseBraceIndent(); Text.UnindentAndWriteCloseBrace();
} }
#endregion #endregion
@ -240,7 +240,7 @@ namespace CppSharp
{ {
public Block RootBlock { get; } public Block RootBlock { get; }
public Block ActiveBlock { get; private set; } public Block ActiveBlock { get; private set; }
public Stack<uint> CurrentIndent => ActiveBlock.Text.CurrentIndent; public Stack<uint> CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
protected BlockGenerator() protected BlockGenerator()
{ {
@ -263,7 +263,7 @@ namespace CppSharp
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null) public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
{ {
var block = new Block { Kind = kind, Object = obj }; var block = new Block { Kind = kind, Object = obj };
CurrentIndent.PushTo(block.Text.CurrentIndent); CurrentIndentation.PushTo(block.Text.CurrentIndentation);
block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine; block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine;
block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine; block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine;
PushBlock(block); PushBlock(block);
@ -335,24 +335,24 @@ namespace CppSharp
ActiveBlock.ResetNewLine(); ActiveBlock.ResetNewLine();
} }
public void PushIndent(uint indent = 4u) public void Indent(uint indentation = 4u)
{ {
ActiveBlock.PushIndent(indent); ActiveBlock.Indent(indentation);
} }
public void PopIndent() public void Unindent()
{ {
ActiveBlock.PopIndent(); ActiveBlock.Unindent();
} }
public void WriteStartBraceIndent() public void WriteOpenBraceAndIndent()
{ {
ActiveBlock.WriteStartBraceIndent(); ActiveBlock.WriteOpenBraceAndIndent();
} }
public void WriteCloseBraceIndent() public void UnindentAndWriteCloseBrace()
{ {
ActiveBlock.WriteCloseBraceIndent(); ActiveBlock.UnindentAndWriteCloseBrace();
} }
#endregion #endregion

36
src/Generator/Utils/TextGenerator.cs

@ -14,20 +14,20 @@ namespace CppSharp
void NewLineIfNeeded(); void NewLineIfNeeded();
void NeedNewLine(); void NeedNewLine();
void ResetNewLine(); void ResetNewLine();
void PushIndent(uint indent = TextGenerator.DefaultIndent); void Indent(uint indentation = TextGenerator.DefaultIndentation);
void PopIndent(); void Unindent();
void WriteStartBraceIndent(); void WriteOpenBraceAndIndent();
void WriteCloseBraceIndent(); void UnindentAndWriteCloseBrace();
} }
public class TextGenerator : ITextGenerator public class TextGenerator : ITextGenerator
{ {
public const uint DefaultIndent = 4; public const uint DefaultIndentation = 4;
public StringBuilder StringBuilder = new StringBuilder(); public StringBuilder StringBuilder = new StringBuilder();
public bool IsStartOfLine { get; set; } public bool IsStartOfLine { get; set; }
public bool NeedsNewLine { get; set; } public bool NeedsNewLine { get; set; }
public Stack<uint> CurrentIndent { get; } = new Stack<uint>(); public Stack<uint> CurrentIndentation { get; } = new Stack<uint>();
public TextGenerator() public TextGenerator()
{ {
@ -38,7 +38,7 @@ namespace CppSharp
StringBuilder = new StringBuilder(generator); StringBuilder = new StringBuilder(generator);
IsStartOfLine = generator.IsStartOfLine; IsStartOfLine = generator.IsStartOfLine;
NeedsNewLine = generator.NeedsNewLine; NeedsNewLine = generator.NeedsNewLine;
CurrentIndent = new Stack<uint>(generator.CurrentIndent); CurrentIndentation = new Stack<uint>(generator.CurrentIndentation);
} }
public TextGenerator Clone() public TextGenerator Clone()
@ -55,7 +55,7 @@ namespace CppSharp
msg = string.Format(msg, args); msg = string.Format(msg, args);
if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg)) if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg))
StringBuilder.Append(new string(' ', (int) CurrentIndent.Sum(u => u))); StringBuilder.Append(new string(' ', (int) CurrentIndentation.Sum(u => u)));
if (msg.Length > 0) if (msg.Length > 0)
IsStartOfLine = msg.EndsWith(Environment.NewLine); IsStartOfLine = msg.EndsWith(Environment.NewLine);
@ -71,9 +71,9 @@ namespace CppSharp
public void WriteLineIndent(string msg, params object[] args) public void WriteLineIndent(string msg, params object[] args)
{ {
PushIndent(); Indent();
WriteLine(msg, args); WriteLine(msg, args);
PopIndent(); Unindent();
} }
public void NewLine() public void NewLine()
@ -100,25 +100,25 @@ namespace CppSharp
NeedsNewLine = false; NeedsNewLine = false;
} }
public void PushIndent(uint indent = DefaultIndent) public void Indent(uint indentation = DefaultIndentation)
{ {
CurrentIndent.Push(indent); CurrentIndentation.Push(indentation);
} }
public void PopIndent() public void Unindent()
{ {
CurrentIndent.Pop(); CurrentIndentation.Pop();
} }
public void WriteStartBraceIndent() public void WriteOpenBraceAndIndent()
{ {
WriteLine("{"); WriteLine("{");
PushIndent(); Indent();
} }
public void WriteCloseBraceIndent() public void UnindentAndWriteCloseBrace()
{ {
PopIndent(); Unindent();
WriteLine("}"); WriteLine("}");
} }

4
src/Generator/Utils/Utils.cs

@ -135,9 +135,9 @@ namespace CppSharp
{ {
var array = new T[source.Count]; var array = new T[source.Count];
source.CopyTo(array, 0); source.CopyTo(array, 0);
foreach (var indent in array.Reverse()) foreach (var element in array.Reverse())
{ {
destination.Push(indent); destination.Push(element);
} }
} }
} }

Loading…
Cancel
Save