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

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

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

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

@ -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> /// <summary>
/// There are two implementation /// There are two implementation
/// for source (CLISources) and header (CLIHeaders) /// for source (CLISources) and header (CLIHeaders)

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

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

4
src/Generator/Passes/ObjectOverridesPass.cs

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

55
src/Generator/Utils/BlockGenerator.cs

@ -13,21 +13,48 @@ namespace CppSharp
IfNotEmpty IfNotEmpty
} }
public class BlockKind public enum BlockKind
{ {
public const int Unknown = 0; Unknown,
public const int BlockComment = 1; BlockComment,
public const int InlineComment = 2; InlineComment,
public const int Header = 3; Header,
public const int Footer = 4; Footer,
public const int EnumItem = 5; Usings,
public const int LAST = 6; 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 class Block : ITextGenerator
{ {
public TextGenerator Text { get; set; } public TextGenerator Text { get; set; }
public int Kind { get; set; } public BlockKind Kind { get; set; }
public NewLineKind NewLineKind { get; set; } public NewLineKind NewLineKind { get; set; }
public object Object { get; set; } public object Object { get; set; }
@ -45,7 +72,7 @@ namespace CppSharp
} }
public Block(int kind) public Block(BlockKind kind)
{ {
Kind = kind; Kind = kind;
Blocks = new List<Block>(); Blocks = new List<Block>();
@ -69,7 +96,7 @@ namespace CppSharp
Blocks.Add(block); Blocks.Add(block);
} }
public IEnumerable<Block> FindBlocks(int kind) public IEnumerable<Block> FindBlocks(BlockKind kind)
{ {
foreach (var block in Blocks) foreach (var block in Blocks)
{ {
@ -306,7 +333,7 @@ namespace CppSharp
ActiveBlock.AddBlock(block); 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 }; var block = new Block { Kind = kind, Object = obj };
PushBlock(block); PushBlock(block);
@ -329,12 +356,12 @@ namespace CppSharp
return block; return block;
} }
public IEnumerable<Block> FindBlocks(int kind) public IEnumerable<Block> FindBlocks(BlockKind kind)
{ {
return RootBlock.FindBlocks(kind); return RootBlock.FindBlocks(kind);
} }
public Block FindBlock(int kind) public Block FindBlock(BlockKind kind)
{ {
return FindBlocks(kind).SingleOrDefault(); return FindBlocks(kind).SingleOrDefault();
} }

Loading…
Cancel
Save