Browse Source

Unify block kinds between generators.

pull/778/head
Joao Matos 9 years ago
parent
commit
d4b6476569
  1. 42
      src/Generator/Generators/CLI/CLIHeaders.cs
  2. 28
      src/Generator/Generators/CLI/CLISources.cs
  3. 25
      src/Generator/Generators/CLI/CLITemplate.cs
  4. 98
      src/Generator/Generators/CSharp/CSharpSources.cs
  5. 4
      src/Generator/Passes/ObjectOverridesPass.cs
  6. 55
      src/Generator/Utils/BlockGenerator.cs

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

@ -23,7 +23,7 @@ namespace CppSharp.Generators.CLI @@ -23,7 +23,7 @@ namespace CppSharp.Generators.CLI
{
GenerateFilePreamble(CommentKind.BCPL);
PushBlock(CLIBlockKind.Includes);
PushBlock(BlockKind.Includes);
WriteLine("#pragma once");
NewLine();
@ -31,14 +31,14 @@ namespace CppSharp.Generators.CLI @@ -31,14 +31,14 @@ namespace CppSharp.Generators.CLI
WriteLine("#include \"CppSharp.h\"");
// Generate #include forward references.
PushBlock(CLIBlockKind.IncludesForwardReferences);
PushBlock(BlockKind.IncludesForwardReferences);
WriteLine("#include <{0}>", TranslationUnit.IncludePath);
GenerateIncludeForwardRefs();
PopBlock(NewLineKind.BeforeNextBlock);
PopBlock(NewLineKind.Always);
// Generate namespace for forward references.
PushBlock(CLIBlockKind.ForwardReferences);
PushBlock(BlockKind.ForwardReferences);
GenerateForwardRefs();
PopBlock(NewLineKind.BeforeNextBlock);
@ -153,7 +153,7 @@ namespace CppSharp.Generators.CLI @@ -153,7 +153,7 @@ namespace CppSharp.Generators.CLI
if (!@enum.IsGenerated || @enum.IsIncomplete)
continue;
PushBlock(CLIBlockKind.Enum, @enum);
PushBlock(BlockKind.Enum, @enum);
GenerateEnum(@enum);
PopBlock(NewLineKind.BeforeNextBlock);
}
@ -170,7 +170,7 @@ namespace CppSharp.Generators.CLI @@ -170,7 +170,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsOpaque)
continue;
PushBlock(CLIBlockKind.Class, @class);
PushBlock(BlockKind.Class, @class);
GenerateClass(@class);
PopBlock(NewLineKind.BeforeNextBlock);
}
@ -190,7 +190,7 @@ namespace CppSharp.Generators.CLI @@ -190,7 +190,7 @@ namespace CppSharp.Generators.CLI
if (generateNamespace)
{
PushBlock(CLIBlockKind.Namespace, @namespace);
PushBlock(BlockKind.Namespace, @namespace);
WriteLine("namespace {0}", isTopLevel
? @namespace.TranslationUnit.Module.OutputNamespace
: @namespace.Name);
@ -219,7 +219,7 @@ namespace CppSharp.Generators.CLI @@ -219,7 +219,7 @@ namespace CppSharp.Generators.CLI
public void GenerateFunctions(DeclarationContext decl)
{
PushBlock(CLIBlockKind.FunctionsClass);
PushBlock(BlockKind.FunctionsClass);
WriteLine("public ref class {0}", TranslationUnit.FileNameWithoutExtension);
WriteLine("{");
@ -272,20 +272,20 @@ namespace CppSharp.Generators.CLI @@ -272,20 +272,20 @@ namespace CppSharp.Generators.CLI
if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{
PushBlock(CLIBlockKind.AccessSpecifier);
PushBlock(BlockKind.AccessSpecifier);
WriteLine("protected:");
PopBlock(NewLineKind.IfNotEmpty);
PushBlock(CLIBlockKind.Fields);
PushBlock(BlockKind.Fields);
WriteLineIndent("bool {0};", Helpers.OwnsNativeInstanceIdentifier);
PopBlock();
}
PushBlock(CLIBlockKind.AccessSpecifier);
PushBlock(BlockKind.AccessSpecifier);
WriteLine("private:");
var accBlock = PopBlock(NewLineKind.IfNotEmpty);
PushBlock(CLIBlockKind.Fields);
PushBlock(BlockKind.Fields);
GenerateClassFields(@class);
var fieldsBlock = PopBlock();
@ -322,7 +322,7 @@ namespace CppSharp.Generators.CLI @@ -322,7 +322,7 @@ namespace CppSharp.Generators.CLI
var functionTemplate = template as FunctionTemplate;
if (functionTemplate == null) continue;
PushBlock(CLIBlockKind.Template);
PushBlock(BlockKind.Template);
var function = functionTemplate.TemplatedFunction;
@ -411,14 +411,14 @@ namespace CppSharp.Generators.CLI @@ -411,14 +411,14 @@ namespace CppSharp.Generators.CLI
private void GenerateClassDestructor(Class @class)
{
PushBlock(CLIBlockKind.Destructor);
PushBlock(BlockKind.Destructor);
WriteLine("~{0}();", @class.Name);
PopBlock(NewLineKind.BeforeNextBlock);
}
private void GenerateClassFinalizer(Class @class)
{
PushBlock(CLIBlockKind.Finalizer);
PushBlock(BlockKind.Finalizer);
WriteLine("!{0}();", @class.Name);
PopBlock(NewLineKind.BeforeNextBlock);
}
@ -451,7 +451,7 @@ namespace CppSharp.Generators.CLI @@ -451,7 +451,7 @@ namespace CppSharp.Generators.CLI
private void GenerateField(Class @class, Field field)
{
PushBlock(CLIBlockKind.Field, field);
PushBlock(BlockKind.Field, field);
GenerateDeclarationCommon(field);
if (@class.IsUnion)
@ -550,7 +550,7 @@ namespace CppSharp.Generators.CLI @@ -550,7 +550,7 @@ namespace CppSharp.Generators.CLI
var type = variable.Type;
PushBlock(CLIBlockKind.Variable);
PushBlock(BlockKind.Variable);
WriteLine("static property {0} {1}", type, variable.Name);
@ -663,7 +663,7 @@ namespace CppSharp.Generators.CLI @@ -663,7 +663,7 @@ namespace CppSharp.Generators.CLI
if (!(property.HasGetter || property.HasSetter))
return;
PushBlock(CLIBlockKind.Property, property);
PushBlock(BlockKind.Property, property);
var type = property.QualifiedType.Visit(TypePrinter);
if (property.IsStatic)
@ -694,7 +694,7 @@ namespace CppSharp.Generators.CLI @@ -694,7 +694,7 @@ namespace CppSharp.Generators.CLI
{
if (ASTUtils.CheckIgnoreMethod(method, Options)) return;
PushBlock(CLIBlockKind.Method, method);
PushBlock(BlockKind.Method, method);
GenerateDeclarationCommon(method);
@ -758,7 +758,7 @@ namespace CppSharp.Generators.CLI @@ -758,7 +758,7 @@ namespace CppSharp.Generators.CLI
FunctionType function;
if (typedef.Type.IsPointerTo(out function))
{
PushBlock(CLIBlockKind.Typedef, typedef);
PushBlock(BlockKind.Typedef, typedef);
GenerateDeclarationCommon(typedef);
var insideClass = typedef.Namespace is Class;
@ -794,7 +794,7 @@ namespace CppSharp.Generators.CLI @@ -794,7 +794,7 @@ namespace CppSharp.Generators.CLI
if (!function.IsGenerated)
return;
PushBlock(CLIBlockKind.Function, function);
PushBlock(BlockKind.Function, function);
GenerateDeclarationCommon(function);
@ -813,7 +813,7 @@ namespace CppSharp.Generators.CLI @@ -813,7 +813,7 @@ namespace CppSharp.Generators.CLI
if (!@enum.IsGenerated || @enum.IsIncomplete)
return;
PushBlock(CLIBlockKind.Enum, @enum);
PushBlock(BlockKind.Enum, @enum);
GenerateDeclarationCommon(@enum);

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -32,14 +32,14 @@ namespace CppSharp.Generators.CLI @@ -32,14 +32,14 @@ namespace CppSharp.Generators.CLI
if (Context.Options.GenerateName != null)
file = Context.Options.GenerateName(TranslationUnit);
PushBlock(CLIBlockKind.Includes);
PushBlock(BlockKind.Includes);
WriteLine("#include \"{0}.h\"", file);
GenerateForwardReferenceHeaders();
NewLine();
PopBlock();
PushBlock(CLIBlockKind.Usings);
PushBlock(BlockKind.Usings);
WriteLine("using namespace System;");
WriteLine("using namespace System::Runtime::InteropServices;");
foreach (var customUsingStatement in Options.DependentNameSpaces)
@ -57,7 +57,7 @@ namespace CppSharp.Generators.CLI @@ -57,7 +57,7 @@ namespace CppSharp.Generators.CLI
public void GenerateForwardReferenceHeaders()
{
PushBlock(CLIBlockKind.IncludesForwardReferences);
PushBlock(BlockKind.IncludesForwardReferences);
var typeReferenceCollector = new CLITypeReferenceCollector(Context.TypeMaps, Context.Options);
typeReferenceCollector.Process(TranslationUnit, filterNamespaces: false);
@ -82,7 +82,7 @@ namespace CppSharp.Generators.CLI @@ -82,7 +82,7 @@ namespace CppSharp.Generators.CLI
private void GenerateDeclContext(DeclarationContext @namespace)
{
PushBlock(CLIBlockKind.Namespace);
PushBlock(BlockKind.Namespace);
foreach (var @class in @namespace.Classes)
{
if (!@class.IsGenerated || @class.IsDependent)
@ -125,7 +125,7 @@ namespace CppSharp.Generators.CLI @@ -125,7 +125,7 @@ namespace CppSharp.Generators.CLI
public void GenerateClass(Class @class)
{
PushBlock(CLIBlockKind.Class);
PushBlock(BlockKind.Class);
GenerateDeclContext(@class);
@ -137,7 +137,7 @@ namespace CppSharp.Generators.CLI @@ -137,7 +137,7 @@ namespace CppSharp.Generators.CLI
{
var qualifiedIdentifier = QualifiedIdentifier(@class);
PushBlock(CLIBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("System::IntPtr {0}::{1}::get()",
qualifiedIdentifier, Helpers.InstanceIdentifier);
WriteStartBraceIndent();
@ -145,7 +145,7 @@ namespace CppSharp.Generators.CLI @@ -145,7 +145,7 @@ namespace CppSharp.Generators.CLI
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CLIBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("void {0}::{1}::set(System::IntPtr object)",
qualifiedIdentifier, Helpers.InstanceIdentifier);
WriteStartBraceIndent();
@ -243,7 +243,7 @@ namespace CppSharp.Generators.CLI @@ -243,7 +243,7 @@ namespace CppSharp.Generators.CLI
private void GenerateClassDestructor(Class @class)
{
PushBlock(CLIBlockKind.Destructor);
PushBlock(BlockKind.Destructor);
WriteLine("{0}::~{1}()", QualifiedIdentifier(@class), @class.Name);
WriteStartBraceIndent();
@ -269,7 +269,7 @@ namespace CppSharp.Generators.CLI @@ -269,7 +269,7 @@ namespace CppSharp.Generators.CLI
private void GenerateClassFinalizer(Class @class)
{
PushBlock(CLIBlockKind.Finalizer);
PushBlock(BlockKind.Finalizer);
WriteLine("{0}::!{1}()", QualifiedIdentifier(@class), @class.Name);
WriteStartBraceIndent();
@ -287,7 +287,7 @@ namespace CppSharp.Generators.CLI @@ -287,7 +287,7 @@ namespace CppSharp.Generators.CLI
var printer = TypePrinter;
var oldCtx = printer.TypePrinterContext;
PushBlock(CLIBlockKind.Template);
PushBlock(BlockKind.Template);
var function = template.TemplatedFunction;
@ -328,7 +328,7 @@ namespace CppSharp.Generators.CLI @@ -328,7 +328,7 @@ namespace CppSharp.Generators.CLI
private void GenerateProperty(Property property, Class realOwner)
{
PushBlock(CLIBlockKind.Property);
PushBlock(BlockKind.Property);
if (property.Field != null)
{
@ -742,7 +742,7 @@ namespace CppSharp.Generators.CLI @@ -742,7 +742,7 @@ namespace CppSharp.Generators.CLI
public void GenerateMethod(Method method, Class @class)
{
PushBlock(CLIBlockKind.Method, method);
PushBlock(BlockKind.Method, method);
if (method.IsConstructor || method.IsDestructor ||
method.OperatorKind == CXXOperatorKind.Conversion ||
@ -761,7 +761,7 @@ namespace CppSharp.Generators.CLI @@ -761,7 +761,7 @@ namespace CppSharp.Generators.CLI
WriteStartBraceIndent();
PushBlock(CLIBlockKind.MethodBody, method);
PushBlock(BlockKind.MethodBody, method);
if (method.IsConstructor && @class.IsRefType)
WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier);

25
src/Generator/Generators/CLI/CLITemplate.cs

@ -25,31 +25,6 @@ namespace CppSharp.Generators.CLI @@ -25,31 +25,6 @@ namespace CppSharp.Generators.CLI
}
}
public class CLIBlockKind
{
public const int Includes = BlockKind.LAST + 1;
public const int IncludesForwardReferences = BlockKind.LAST + 2;
public const int Namespace = BlockKind.LAST + 3;
public const int ForwardReferences = BlockKind.LAST + 4;
public const int Enum = BlockKind.LAST + 5;
public const int EnumItem = BlockKind.LAST + 6;
public const int Class = BlockKind.LAST + 7;
public const int Method = BlockKind.LAST + 8;
public const int MethodBody = BlockKind.LAST + 9;
public const int Usings = BlockKind.LAST + 10;
public const int FunctionsClass = BlockKind.LAST + 11;
public const int Function = BlockKind.LAST + 12;
public const int Property = BlockKind.LAST + 13;
public const int Typedef = BlockKind.LAST + 14;
public const int Variable = BlockKind.LAST + 15;
public const int Template = BlockKind.LAST + 16;
public static int Destructor = BlockKind.LAST + 17;
public static int Finalizer = BlockKind.LAST + 18;
public static int AccessSpecifier = BlockKind.LAST + 19;
public static int Fields = BlockKind.LAST + 20;
public static int Field = BlockKind.LAST + 21;
}
/// <summary>
/// There are two implementation
/// for source (CLISources) and header (CLIHeaders)

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

@ -14,30 +14,6 @@ using Type = CppSharp.AST.Type; @@ -14,30 +14,6 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.CSharp
{
public class CSharpBlockKind
{
private const int FIRST = BlockKind.LAST + 1000;
public const int Usings = FIRST + 1;
public const int Namespace = FIRST + 2;
public const int Enum = FIRST + 3;
public const int Typedef = FIRST + 4;
public const int Class = FIRST + 5;
public const int InternalsClass = FIRST + 6;
public const int InternalsClassMethod = FIRST + 7;
public const int InternalsClassField = FIRST + 15;
public const int Functions = FIRST + 8;
public const int Function = FIRST + 9;
public const int Method = FIRST + 10;
public const int Event = FIRST + 11;
public const int Variable = FIRST + 12;
public const int Property = FIRST + 13;
public const int Field = FIRST + 14;
public const int VTableDelegate = FIRST + 16;
public const int Region = FIRST + 17;
public const int Interface = FIRST + 18;
public const int Finalizer = FIRST + 19;
}
public class CSharpSources : CodeGenerator
{
public CSharpTypePrinter TypePrinter { get; set; }
@ -92,7 +68,7 @@ namespace CppSharp.Generators.CSharp @@ -92,7 +68,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrEmpty(Module.OutputNamespace))
{
PushBlock(CSharpBlockKind.Namespace);
PushBlock(BlockKind.Namespace);
WriteLine("namespace {0}", Module.OutputNamespace);
WriteStartBraceIndent();
}
@ -109,7 +85,7 @@ namespace CppSharp.Generators.CSharp @@ -109,7 +85,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateUsings()
{
PushBlock(CSharpBlockKind.Usings);
PushBlock(BlockKind.Usings);
WriteLine("using System;");
WriteLine("using System.Runtime.InteropServices;");
WriteLine("using System.Security;");
@ -143,7 +119,7 @@ namespace CppSharp.Generators.CSharp @@ -143,7 +119,7 @@ namespace CppSharp.Generators.CSharp
if (shouldGenerateNamespace)
{
PushBlock(CSharpBlockKind.Namespace);
PushBlock(BlockKind.Namespace);
WriteLine("namespace {0}", context.Name);
WriteStartBraceIndent();
}
@ -189,12 +165,12 @@ namespace CppSharp.Generators.CSharp @@ -189,12 +165,12 @@ namespace CppSharp.Generators.CSharp
if (!context.Functions.Any(f => f.IsGenerated) && !hasGlobalVariables)
return;
PushBlock(CSharpBlockKind.Functions);
PushBlock(BlockKind.Functions);
var parentName = SafeIdentifier(context.TranslationUnit.FileNameWithoutExtension);
WriteLine("public unsafe partial class {0}", parentName);
WriteStartBraceIndent();
PushBlock(CSharpBlockKind.InternalsClass);
PushBlock(BlockKind.InternalsClass);
GenerateClassInternalHead();
WriteStartBraceIndent();
@ -247,7 +223,7 @@ namespace CppSharp.Generators.CSharp @@ -247,7 +223,7 @@ namespace CppSharp.Generators.CSharp
bool generateClass = specializations.Any(s => s.IsGenerated);
if (!generateClass)
{
PushBlock(CSharpBlockKind.Namespace);
PushBlock(BlockKind.Namespace);
WriteLine("namespace {0}{1}",
classTemplate.OriginalNamespace is Class ?
classTemplate.OriginalNamespace.Name + '_' : string.Empty,
@ -327,7 +303,7 @@ namespace CppSharp.Generators.CSharp @@ -327,7 +303,7 @@ namespace CppSharp.Generators.CSharp
}
}
PushBlock(CSharpBlockKind.Class);
PushBlock(BlockKind.Class);
GenerateDeclarationCommon(@class);
GenerateClassProlog(@class);
@ -347,7 +323,7 @@ namespace CppSharp.Generators.CSharp @@ -347,7 +323,7 @@ namespace CppSharp.Generators.CSharp
if (ShouldGenerateClassNativeField(@class))
{
PushBlock(CSharpBlockKind.Field);
PushBlock(BlockKind.Field);
if (@class.IsValueType)
{
WriteLine("private {0}.{1} {2};", @class.Name, Helpers.InternalStruct,
@ -362,7 +338,7 @@ namespace CppSharp.Generators.CSharp @@ -362,7 +338,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Field);
PushBlock(BlockKind.Field);
WriteLine("protected int {0};", Helpers.PointerAdjustmentIdentifier);
@ -400,7 +376,7 @@ namespace CppSharp.Generators.CSharp @@ -400,7 +376,7 @@ namespace CppSharp.Generators.CSharp
if (!@class.IsGenerated || @class.IsIncomplete)
return;
PushBlock(CSharpBlockKind.Interface);
PushBlock(BlockKind.Interface);
GenerateDeclarationCommon(@class);
GenerateClassProlog(@class);
@ -411,7 +387,7 @@ namespace CppSharp.Generators.CSharp @@ -411,7 +387,7 @@ namespace CppSharp.Generators.CSharp
foreach (var method in @class.Methods.Where(m =>
!ASTUtils.CheckIgnoreMethod(m, Options) && m.Access == AccessSpecifier.Public))
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
GenerateDeclarationCommon(method);
var functionName = GetMethodIdentifier(method);
@ -426,7 +402,7 @@ namespace CppSharp.Generators.CSharp @@ -426,7 +402,7 @@ namespace CppSharp.Generators.CSharp
}
foreach (var prop in @class.Properties.Where(p => p.IsGenerated && p.Access == AccessSpecifier.Public))
{
PushBlock(CSharpBlockKind.Property);
PushBlock(BlockKind.Property);
var type = prop.Type;
if (prop.Parameters.Count > 0 && prop.Type.IsPointerToPrimitiveType())
type = ((PointerType) prop.Type).Pointee;
@ -447,7 +423,7 @@ namespace CppSharp.Generators.CSharp @@ -447,7 +423,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateClassInternals(Class @class)
{
PushBlock(CSharpBlockKind.InternalsClass);
PushBlock(BlockKind.InternalsClass);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.Size})]");
// no else because the layout is sequential for structures by default
@ -658,7 +634,7 @@ namespace CppSharp.Generators.CSharp @@ -658,7 +634,7 @@ namespace CppSharp.Generators.CSharp
safeIdentifier = SafeIdentifier(field.Name);
}
PushBlock(CSharpBlockKind.Field);
PushBlock(BlockKind.Field);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[FieldOffset({field.Offset})]");
@ -690,7 +666,7 @@ namespace CppSharp.Generators.CSharp @@ -690,7 +666,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassField(Field field, bool @public = false)
{
PushBlock(CSharpBlockKind.Field);
PushBlock(BlockKind.Field);
GenerateDeclarationCommon(field);
@ -706,7 +682,7 @@ namespace CppSharp.Generators.CSharp @@ -706,7 +682,7 @@ namespace CppSharp.Generators.CSharp
Class @class, bool isAbstract = false, Property property = null)
where T : Declaration, ITypedDecl
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
Write("set");
var param = new Parameter
@ -934,7 +910,7 @@ namespace CppSharp.Generators.CSharp @@ -934,7 +910,7 @@ namespace CppSharp.Generators.CSharp
Class @class, bool isAbstract = false, Property property = null)
where T : Declaration, ITypedDecl
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
Write("get");
if (property != null && property.GetMethod != null &&
@ -1171,7 +1147,7 @@ namespace CppSharp.Generators.CSharp @@ -1171,7 +1147,7 @@ namespace CppSharp.Generators.CSharp
continue;
}
PushBlock(CSharpBlockKind.Property);
PushBlock(BlockKind.Property);
ArrayType arrayType = prop.Type as ArrayType;
if (arrayType != null && arrayType.Type.IsPointerToPrimitiveType() && prop.Field != null)
@ -1252,7 +1228,7 @@ namespace CppSharp.Generators.CSharp @@ -1252,7 +1228,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateVariable(Class @class, Variable variable)
{
PushBlock(CSharpBlockKind.Variable);
PushBlock(BlockKind.Variable);
GenerateDeclarationCommon(variable);
WriteLine("public static {0} {1}", variable.Type, variable.Name);
@ -1286,7 +1262,7 @@ namespace CppSharp.Generators.CSharp @@ -1286,7 +1262,7 @@ namespace CppSharp.Generators.CSharp
if (wrappedEntries.Count == 0)
return;
PushBlock(CSharpBlockKind.Region);
PushBlock(BlockKind.Region);
WriteLine("#region Virtual table interop");
NewLine();
@ -1599,7 +1575,7 @@ namespace CppSharp.Generators.CSharp @@ -1599,7 +1575,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateVTableMethodDelegates(Class @class, Method method)
{
PushBlock(CSharpBlockKind.VTableDelegate);
PushBlock(BlockKind.VTableDelegate);
// This works around a parser bug, see https://github.com/mono/CppSharp/issues/202
if (method.Signature != null)
@ -1657,7 +1633,7 @@ namespace CppSharp.Generators.CSharp @@ -1657,7 +1633,7 @@ namespace CppSharp.Generators.CSharp
if (!@event.IsGenerated)
return true;
PushBlock(CSharpBlockKind.Event, @event);
PushBlock(BlockKind.Event, @event);
TypePrinter.PushContext(TypePrinterContextKind.Native);
var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true);
TypePrinter.PopContext();
@ -1800,7 +1776,7 @@ namespace CppSharp.Generators.CSharp @@ -1800,7 +1776,7 @@ namespace CppSharp.Generators.CSharp
if (!Options.GenerateFinalizers)
return;
PushBlock(CSharpBlockKind.Finalizer);
PushBlock(BlockKind.Finalizer);
WriteLine("~{0}()", @class.Name);
WriteStartBraceIndent();
@ -1817,7 +1793,7 @@ namespace CppSharp.Generators.CSharp @@ -1817,7 +1793,7 @@ namespace CppSharp.Generators.CSharp
// Generate the IDispose Dispose() method.
if (!hasBaseClass)
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("public void Dispose()");
WriteStartBraceIndent();
@ -1830,7 +1806,7 @@ namespace CppSharp.Generators.CSharp @@ -1830,7 +1806,7 @@ namespace CppSharp.Generators.CSharp
}
// Generate Dispose(bool) method
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
Write("public ");
if (!@class.IsValueType)
Write(hasBaseClass ? "override " : "virtual ");
@ -1910,7 +1886,7 @@ namespace CppSharp.Generators.CSharp @@ -1910,7 +1886,7 @@ namespace CppSharp.Generators.CSharp
var shouldGenerateClassNativeField = ShouldGenerateClassNativeField(@class);
if (@class.IsRefType && shouldGenerateClassNativeField)
{
PushBlock(CSharpBlockKind.Field);
PushBlock(BlockKind.Field);
WriteLine("protected bool {0};", Helpers.OwnsNativeInstanceIdentifier);
PopBlock(NewLineKind.BeforeNextBlock);
}
@ -1920,7 +1896,7 @@ namespace CppSharp.Generators.CSharp @@ -1920,7 +1896,7 @@ namespace CppSharp.Generators.CSharp
var ctorCall = string.Format("{0}{1}", @class.Name, @class.IsAbstract ? "Internal" : "");
if (!@class.IsAbstractImpl)
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("internal static {0}{1} {2}(global::System.IntPtr native, bool skipVTables = false)",
@class.NeedsBase && !@class.BaseClass.IsInterface ? "new " : string.Empty,
@class.Visit(TypePrinter), Helpers.CreateInstanceIdentifier);
@ -1932,7 +1908,7 @@ namespace CppSharp.Generators.CSharp @@ -1932,7 +1908,7 @@ namespace CppSharp.Generators.CSharp
GenerateNativeConstructorByValue(@class, ctorCall);
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("{0} {1}(void* native, bool skipVTables = false){2}",
@class.IsAbstractImpl ? "internal" : (@class.IsRefType ? "protected" : "private"),
@class.Name, @class.IsValueType ? " : this()" : string.Empty);
@ -1995,7 +1971,7 @@ namespace CppSharp.Generators.CSharp @@ -1995,7 +1971,7 @@ namespace CppSharp.Generators.CSharp
if (!@class.IsAbstractImpl)
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("internal static {0} {1}({2} native, bool skipVTables = false)",
@class.Visit(TypePrinter), Helpers.CreateInstanceIdentifier, @internal);
WriteStartBraceIndent();
@ -2006,7 +1982,7 @@ namespace CppSharp.Generators.CSharp @@ -2006,7 +1982,7 @@ namespace CppSharp.Generators.CSharp
if (@class.IsRefType && !@class.IsAbstract)
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine($"private static void* __CopyValue({@internal} native)");
WriteStartBraceIndent();
var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
@ -2034,7 +2010,7 @@ namespace CppSharp.Generators.CSharp @@ -2034,7 +2010,7 @@ namespace CppSharp.Generators.CSharp
}
if (!@class.IsAbstract)
{
PushBlock(CSharpBlockKind.Method);
PushBlock(BlockKind.Method);
WriteLine("{0} {1}({2} native, bool skipVTables = false)",
@class.IsAbstractImpl ? "internal" : "private", @class.Name, @internal);
WriteLineIndent(@class.IsRefType ? ": this(__CopyValue(native), skipVTables)" : ": this()");
@ -2078,7 +2054,7 @@ namespace CppSharp.Generators.CSharp @@ -2078,7 +2054,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateFunction(Function function, string parentName)
{
PushBlock(CSharpBlockKind.Function);
PushBlock(BlockKind.Function);
GenerateDeclarationCommon(function);
var functionName = GetFunctionIdentifier(function);
@ -2100,7 +2076,7 @@ namespace CppSharp.Generators.CSharp @@ -2100,7 +2076,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateMethod(Method method, Class @class)
{
PushBlock(CSharpBlockKind.Method, method);
PushBlock(BlockKind.Method, method);
GenerateDeclarationCommon(method);
if (method.ExplicitInterfaceImpl == null)
@ -2898,13 +2874,13 @@ namespace CppSharp.Generators.CSharp @@ -2898,13 +2874,13 @@ namespace CppSharp.Generators.CSharp
if (typedef.Type.IsPointerToPrimitiveType(PrimitiveType.Void)
|| typedef.Type.IsPointerTo(out tag))
{
PushBlock(CSharpBlockKind.Typedef);
PushBlock(BlockKind.Typedef);
WriteLine("public class " + typedef.Name + @" { }");
PopBlock(NewLineKind.BeforeNextBlock);
}
else if (typedef.Type.IsPointerTo(out functionType))
{
PushBlock(CSharpBlockKind.Typedef);
PushBlock(BlockKind.Typedef);
var attributedType = typedef.Type.GetPointee() as AttributedType;
var callingConvention = attributedType == null
? functionType.CallingConvention
@ -2934,7 +2910,7 @@ namespace CppSharp.Generators.CSharp @@ -2934,7 +2910,7 @@ namespace CppSharp.Generators.CSharp
if (@enum.IsIncomplete)
return true;
PushBlock(CSharpBlockKind.Enum);
PushBlock(BlockKind.Enum);
GenerateDeclarationCommon(@enum);
if (@enum.IsFlags)
@ -3025,7 +3001,7 @@ namespace CppSharp.Generators.CSharp @@ -3025,7 +3001,7 @@ namespace CppSharp.Generators.CSharp
function = function.OriginalFunction;
}
PushBlock(CSharpBlockKind.InternalsClassMethod);
PushBlock(BlockKind.InternalsClassMethod);
WriteLine("[SuppressUnmanagedCodeSecurity]");
Write("[DllImport(\"{0}\", ", GetLibraryOf(function));

4
src/Generator/Passes/ObjectOverridesPass.cs

@ -28,7 +28,7 @@ namespace CppSharp @@ -28,7 +28,7 @@ namespace CppSharp
needsStreamInclude = false;
foreach (var template in output.Outputs)
{
foreach (var block in template.FindBlocks(CLIBlockKind.MethodBody))
foreach (var block in template.FindBlocks(BlockKind.MethodBody))
{
var method = block.Object as Method;
VisitMethod(method, block);
@ -38,7 +38,7 @@ namespace CppSharp @@ -38,7 +38,7 @@ namespace CppSharp
var sourcesTemplate = template as CLISources;
if (sourcesTemplate != null)
{
foreach (var block in sourcesTemplate.FindBlocks(CLIBlockKind.Includes))
foreach (var block in sourcesTemplate.FindBlocks(BlockKind.Includes))
{
block.WriteLine("#include <sstream>");
block.WriteLine("");

55
src/Generator/Utils/BlockGenerator.cs

@ -13,21 +13,48 @@ namespace CppSharp @@ -13,21 +13,48 @@ namespace CppSharp
IfNotEmpty
}
public class BlockKind
public enum BlockKind
{
public const int Unknown = 0;
public const int BlockComment = 1;
public const int InlineComment = 2;
public const int Header = 3;
public const int Footer = 4;
public const int EnumItem = 5;
public const int LAST = 6;
Unknown,
BlockComment,
InlineComment,
Header,
Footer,
Usings,
Namespace,
Enum,
EnumItem,
Typedef,
Class,
InternalsClass,
InternalsClassMethod,
InternalsClassField,
Functions,
Function,
Method,
Event,
Variable,
Property,
Field,
VTableDelegate,
Region,
Interface,
Finalizer,
Includes,
IncludesForwardReferences,
ForwardReferences,
MethodBody,
FunctionsClass,
Template,
Destructor,
AccessSpecifier,
Fields,
}
public class Block : ITextGenerator
{
public TextGenerator Text { get; set; }
public int Kind { get; set; }
public BlockKind Kind { get; set; }
public NewLineKind NewLineKind { get; set; }
public object Object { get; set; }
@ -45,7 +72,7 @@ namespace CppSharp @@ -45,7 +72,7 @@ namespace CppSharp
}
public Block(int kind)
public Block(BlockKind kind)
{
Kind = kind;
Blocks = new List<Block>();
@ -69,7 +96,7 @@ namespace CppSharp @@ -69,7 +96,7 @@ namespace CppSharp
Blocks.Add(block);
}
public IEnumerable<Block> FindBlocks(int kind)
public IEnumerable<Block> FindBlocks(BlockKind kind)
{
foreach (var block in Blocks)
{
@ -306,7 +333,7 @@ namespace CppSharp @@ -306,7 +333,7 @@ namespace CppSharp
ActiveBlock.AddBlock(block);
}
public void PushBlock(int kind = 0, object obj = null)
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
{
var block = new Block { Kind = kind, Object = obj };
PushBlock(block);
@ -329,12 +356,12 @@ namespace CppSharp @@ -329,12 +356,12 @@ namespace CppSharp
return block;
}
public IEnumerable<Block> FindBlocks(int kind)
public IEnumerable<Block> FindBlocks(BlockKind kind)
{
return RootBlock.FindBlocks(kind);
}
public Block FindBlock(int kind)
public Block FindBlock(BlockKind kind)
{
return FindBlocks(kind).SingleOrDefault();
}

Loading…
Cancel
Save