Browse Source

Convert the C# backend to the new blocks system.

pull/13/merge
triton 12 years ago
parent
commit
f47dc5bb07
  1. 159
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

159
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -4,7 +4,6 @@ using System.Globalization; @@ -4,7 +4,6 @@ using System.Globalization;
using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.Passes;
namespace CppSharp.Generators.CSharp
{
@ -65,7 +64,20 @@ namespace CppSharp.Generators.CSharp @@ -65,7 +64,20 @@ namespace CppSharp.Generators.CSharp
public class CSharpBlockKind
{
public const int Usings = BlockKind.LAST + 1;
public const int Namespace = BlockKind.LAST + 2;
public const int Enum = BlockKind.LAST + 3;
public const int Typedef = BlockKind.LAST + 4;
public const int Class = BlockKind.LAST + 5;
public const int InternalsClass = BlockKind.LAST + 6;
public const int InternalsClassMethod = BlockKind.LAST + 7;
public const int Functions = BlockKind.LAST + 8;
public const int Function = BlockKind.LAST + 9;
public const int Method = BlockKind.LAST + 10;
public const int Event = BlockKind.LAST + 11;
public const int Variable = BlockKind.LAST + 12;
public const int Property = BlockKind.LAST + 13;
public const int Field = BlockKind.LAST + 14;
}
public class CSharpTextTemplate : Template
@ -121,13 +133,15 @@ namespace CppSharp.Generators.CSharp @@ -121,13 +133,15 @@ namespace CppSharp.Generators.CSharp
{
GenerateHeader();
PushBlock(CSharpBlockKind.Usings);
WriteLine("using System;");
WriteLine("using System.Runtime.InteropServices;");
WriteLine("using System.Security;");
NewLine();
PopBlock(NewLineKind.BeforeNextBlock);
if (Options.GenerateLibraryNamespace)
{
PushBlock(CSharpBlockKind.Namespace);
WriteLine("namespace {0}", SafeIdentifier(Driver.Options.OutputNamespace));
WriteStartBraceIndent();
}
@ -135,15 +149,20 @@ namespace CppSharp.Generators.CSharp @@ -135,15 +149,20 @@ namespace CppSharp.Generators.CSharp
GenerateDeclContext(TranslationUnit);
if (Options.GenerateLibraryNamespace)
{
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
public void GenerateHeader()
{
PushBlock(BlockKind.Header);
WriteLine("//----------------------------------------------------------------------------");
WriteLine("// This is autogenerated code by CppSharp.");
WriteLine("// Do not edit this file or all your changes will be lost after re-generation.");
WriteLine("//----------------------------------------------------------------------------");
PopBlock();
}
private void GenerateDeclContext(DeclarationContext context)
@ -155,6 +174,7 @@ namespace CppSharp.Generators.CSharp @@ -155,6 +174,7 @@ namespace CppSharp.Generators.CSharp
if (shouldGenerateNamespace)
{
PushBlock(CSharpBlockKind.Namespace);
WriteLine("namespace {0}", context.Name);
WriteStartBraceIndent();
}
@ -165,9 +185,7 @@ namespace CppSharp.Generators.CSharp @@ -165,9 +185,7 @@ namespace CppSharp.Generators.CSharp
if (@enum.Ignore || @enum.IsIncomplete)
continue;
NewLineIfNeeded();
GenerateEnum(@enum);
NeedNewLine();
}
// Generate all the typedef declarations.
@ -175,12 +193,7 @@ namespace CppSharp.Generators.CSharp @@ -175,12 +193,7 @@ namespace CppSharp.Generators.CSharp
{
if (typedef.Ignore) continue;
NewLineIfNeeded();
if (!GenerateTypedef(typedef))
continue;
NeedNewLine();
GenerateTypedef(typedef);
}
// Generate all the struct/class declarations.
@ -192,18 +205,17 @@ namespace CppSharp.Generators.CSharp @@ -192,18 +205,17 @@ namespace CppSharp.Generators.CSharp
if (@class.IsDependent)
continue;
NewLineIfNeeded();
GenerateClass(@class);
NeedNewLine();
}
if (context.HasFunctions)
{
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Functions);
WriteLine("public partial class {0}{1}", SafeIdentifier(Options.OutputNamespace),
TranslationUnit.FileNameWithoutExtension);
WriteStartBraceIndent();
PushBlock(CSharpBlockKind.InternalsClass);
GenerateClassInternalHead();
WriteStartBraceIndent();
@ -212,39 +224,38 @@ namespace CppSharp.Generators.CSharp @@ -212,39 +224,38 @@ namespace CppSharp.Generators.CSharp
{
if (function.Ignore) continue;
NewLineIfNeeded();
GenerateInternalFunction(function);
NeedNewLine();
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
foreach (var function in context.Functions)
{
if (function.Ignore) continue;
NewLineIfNeeded();
GenerateFunction(function);
NeedNewLine();
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
foreach (var @event in context.Events)
{
if (@event.Ignore) continue;
NewLineIfNeeded();
GenerateEvent(@event);
NeedNewLine();
}
foreach(var childNamespace in context.Namespaces)
GenerateDeclContext(childNamespace);
if (shouldGenerateNamespace)
{
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
public void GenerateDeclarationCommon(Declaration decl)
@ -288,6 +299,7 @@ namespace CppSharp.Generators.CSharp @@ -288,6 +299,7 @@ namespace CppSharp.Generators.CSharp
if (@class.Ignore || @class.IsIncomplete)
return;
PushBlock(CSharpBlockKind.Class);
GenerateDeclarationCommon(@class);
if (@class.IsUnion)
@ -309,10 +321,10 @@ namespace CppSharp.Generators.CSharp @@ -309,10 +321,10 @@ namespace CppSharp.Generators.CSharp
if (ShouldGenerateClassNativeField(@class))
{
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Field);
WriteLine("public System.IntPtr {0} {{ get; protected set; }}",
Helpers.InstanceIdentifier);
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
GenerateClassConstructors(@class);
@ -323,23 +335,38 @@ namespace CppSharp.Generators.CSharp @@ -323,23 +335,38 @@ namespace CppSharp.Generators.CSharp
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
public void GenerateClassInternals(Class @class)
{
PushBlock(CSharpBlockKind.InternalsClass);
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]",
@class.Layout.Size);
GenerateClassInternalHead(@class);
WriteStartBraceIndent();
ResetNewLine();
var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
GenerateClassFields(@class, isInternal: true);
var functions = GatherClassInternalFunctions(@class);
foreach (var function in functions)
{
GenerateInternalFunction(function);
}
typePrinter.PopContext();
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
private static HashSet<Function> GatherClassInternalFunctions(Class @class)
{
var functions = new HashSet<Function>();
foreach (var ctor in @class.Constructors)
@ -378,17 +405,7 @@ namespace CppSharp.Generators.CSharp @@ -378,17 +405,7 @@ namespace CppSharp.Generators.CSharp
if (prop.SetMethod != null)
functions.Add(prop.SetMethod);
}
foreach (var function in functions)
{
NewLineIfNeeded();
GenerateInternalFunction(function);
}
typePrinter.PopContext();
WriteCloseBraceIndent();
NeedNewLine();
return functions;
}
private void GenerateClassInternalHead(Class @class = null)
@ -565,7 +582,7 @@ namespace CppSharp.Generators.CSharp @@ -565,7 +582,7 @@ namespace CppSharp.Generators.CSharp
{
if (ASTUtils.CheckIgnoreField(@class, field)) continue;
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Field);
if (isInternal)
{
@ -592,7 +609,7 @@ namespace CppSharp.Generators.CSharp @@ -592,7 +609,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("public {0} {1};", field.Type, SafeIdentifier(field.Name));
}
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
@ -602,16 +619,17 @@ namespace CppSharp.Generators.CSharp @@ -602,16 +619,17 @@ namespace CppSharp.Generators.CSharp
{
var @class = field.Class;
PushBlock(CSharpBlockKind.Property);
GenerateDeclarationCommon(field);
WriteLine("public {0} {1}", field.Type, SafeIdentifier(field.Name));
WriteStartBraceIndent();
GeneratePropertyGetter(field, @class);
NewLine();
GeneratePropertySetter(field, @class);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
private Tuple<string, string> GetDeclarationLibrarySymbol(IMangledDecl decl)
@ -638,6 +656,7 @@ namespace CppSharp.Generators.CSharp @@ -638,6 +656,7 @@ namespace CppSharp.Generators.CSharp
private void GeneratePropertySetter<T>(T decl, Class @class)
where T : Declaration, ITypedDecl
{
PushBlock(CSharpBlockKind.Method);
WriteLine("set");
WriteStartBraceIndent();
@ -685,11 +704,13 @@ namespace CppSharp.Generators.CSharp @@ -685,11 +704,13 @@ namespace CppSharp.Generators.CSharp
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
private void GeneratePropertyGetter<T>(T decl, Class @class)
where T : Declaration, ITypedDecl
{
PushBlock(CSharpBlockKind.Method);
WriteLine("get");
WriteStartBraceIndent();
@ -758,6 +779,7 @@ namespace CppSharp.Generators.CSharp @@ -758,6 +779,7 @@ namespace CppSharp.Generators.CSharp
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
public void GenerateClassMethods(Class @class)
@ -777,16 +799,12 @@ namespace CppSharp.Generators.CSharp @@ -777,16 +799,12 @@ namespace CppSharp.Generators.CSharp
continue;
}
NewLineIfNeeded();
GenerateMethod(method, @class);
NeedNewLine();
}
foreach (var method in staticMethods)
{
NewLineIfNeeded();
GenerateMethod(method, @class);
NeedNewLine();
}
}
@ -801,9 +819,7 @@ namespace CppSharp.Generators.CSharp @@ -801,9 +819,7 @@ namespace CppSharp.Generators.CSharp
var type = variable.Type;
NewLineIfNeeded();
GenerateVariable(@class, type, variable);
NeedNewLine();
}
}
@ -813,26 +829,25 @@ namespace CppSharp.Generators.CSharp @@ -813,26 +829,25 @@ namespace CppSharp.Generators.CSharp
{
if (prop.Ignore) continue;
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Property);
WriteLine("public {0} {1}", prop.Type, prop.Name);
WriteStartBraceIndent();
GeneratePropertyGetter(prop.GetMethod, @class);
NeedNewLine();
if (prop.SetMethod != null)
{
NewLineIfNeeded();
GeneratePropertySetter(prop.SetMethod, @class);
}
WriteCloseBraceIndent();
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
private void GenerateVariable(Class @class, AST.Type type, Variable variable)
{
PushBlock(CSharpBlockKind.Variable);
WriteLine("public static {0} {1}", type, variable.Name);
WriteStartBraceIndent();
@ -842,6 +857,7 @@ namespace CppSharp.Generators.CSharp @@ -842,6 +857,7 @@ namespace CppSharp.Generators.CSharp
GeneratePropertySetter(variable, @class);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
#region Events
@ -852,6 +868,7 @@ namespace CppSharp.Generators.CSharp @@ -852,6 +868,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateEvent(Event @event)
{
PushBlock(CSharpBlockKind.Event);
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);
var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true);
TypePrinter.PopContext();
@ -878,7 +895,7 @@ namespace CppSharp.Generators.CSharp @@ -878,7 +895,7 @@ namespace CppSharp.Generators.CSharp
NewLine();
GenerateEventRaiseWrapper(@event);
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
private void GenerateEventAdd(Event @event)
@ -956,8 +973,6 @@ namespace CppSharp.Generators.CSharp @@ -956,8 +973,6 @@ namespace CppSharp.Generators.CSharp
public void GenerateClassConstructors(Class @class)
{
NewLineIfNeeded();
// Output a default constructor that takes the native pointer.
GenerateNativeConstructor(@class);
@ -966,9 +981,7 @@ namespace CppSharp.Generators.CSharp @@ -966,9 +981,7 @@ namespace CppSharp.Generators.CSharp
if (ASTUtils.CheckIgnoreMethod(@class, ctor))
continue;
NewLineIfNeeded();
GenerateMethod(ctor, @class);
NeedNewLine();
}
if (@class.IsRefType)
@ -982,7 +995,7 @@ namespace CppSharp.Generators.CSharp @@ -982,7 +995,7 @@ namespace CppSharp.Generators.CSharp
// Generate the IDispose Dispose() method.
if (!hasBaseClass)
{
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Method);
WriteLine("public void Dispose()");
WriteStartBraceIndent();
@ -990,11 +1003,11 @@ namespace CppSharp.Generators.CSharp @@ -990,11 +1003,11 @@ namespace CppSharp.Generators.CSharp
WriteLine("GC.SuppressFinalize(this);");
WriteCloseBraceIndent();
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
// Generate Dispose(bool) method
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Method);
Write("protected ");
Write(hasBaseClass ? "override " : "virtual ");
@ -1009,25 +1022,28 @@ namespace CppSharp.Generators.CSharp @@ -1009,25 +1022,28 @@ namespace CppSharp.Generators.CSharp
WriteLine("base.Dispose(disposing);");
WriteCloseBraceIndent();
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
private void GenerateNativeConstructor(Class @class)
{
PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}({1}.Internal* native)", SafeIdentifier(@class.Name),
@class.Name);
WriteLineIndent(": this(new System.IntPtr(native))");
WriteStartBraceIndent();
WriteCloseBraceIndent();
NewLine();
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}({1}.Internal native)", SafeIdentifier(@class.Name),
@class.Name);
WriteLineIndent(": this(&native)");
WriteStartBraceIndent();
WriteCloseBraceIndent();
NewLine();
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}(System.IntPtr native)", SafeIdentifier(@class.Name));
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
@ -1049,23 +1065,24 @@ namespace CppSharp.Generators.CSharp @@ -1049,23 +1065,24 @@ namespace CppSharp.Generators.CSharp
}
WriteCloseBraceIndent();
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
if (@class.IsValueType)
{
NewLineIfNeeded();
PushBlock(CSharpBlockKind.Method);
WriteLine("internal Internal ToInternal()");
WriteStartBraceIndent();
GenerateStructInternalMarshaling(@class);
WriteCloseBraceIndent();
NewLine();
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method);
WriteLine("internal void FromInternal(Internal* native)");
WriteStartBraceIndent();
WriteLine("var {0} = {1};", Helpers.GeneratedIdentifier("ptr"), "native");
GenerateStructMarshalingFields(@class);
WriteCloseBraceIndent();
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
@ -1099,6 +1116,7 @@ namespace CppSharp.Generators.CSharp @@ -1099,6 +1116,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateFunction(Function function)
{
PushBlock(CSharpBlockKind.Function);
GenerateDeclarationCommon(function);
var functionName = GetFunctionIdentifier(function);
@ -1110,10 +1128,12 @@ namespace CppSharp.Generators.CSharp @@ -1110,10 +1128,12 @@ namespace CppSharp.Generators.CSharp
GenerateInternalFunctionCall(function);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
public void GenerateMethod(Method method, Class @class)
{
PushBlock(CSharpBlockKind.Method);
GenerateDeclarationCommon(method);
Write("public ");
@ -1173,6 +1193,7 @@ namespace CppSharp.Generators.CSharp @@ -1173,6 +1193,7 @@ namespace CppSharp.Generators.CSharp
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
private static string GetOperatorOverloadPair(CXXOperatorKind kind)
@ -1528,14 +1549,17 @@ namespace CppSharp.Generators.CSharp @@ -1528,14 +1549,17 @@ namespace CppSharp.Generators.CSharp
if (typedef.Type.IsPointerToPrimitiveType(PrimitiveType.Void)
|| typedef.Type.IsPointerTo<TagType>(out tag))
{
PushBlock(CSharpBlockKind.Typedef);
WriteLine("public class " + SafeIdentifier(typedef.Name) + @" { }");
PopBlock(NewLineKind.BeforeNextBlock);
}
else if (typedef.Type.IsPointerTo<FunctionType>(out function))
{
PushBlock(CSharpBlockKind.Typedef);
WriteLine("public {0};",
string.Format(TypePrinter.VisitDelegate(function).Type,
SafeIdentifier(typedef.Name)));
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
}
else if (typedef.Type.IsEnumType())
{
@ -1554,6 +1578,8 @@ namespace CppSharp.Generators.CSharp @@ -1554,6 +1578,8 @@ namespace CppSharp.Generators.CSharp
public void GenerateEnum(Enumeration @enum)
{
if (@enum.Ignore) return;
PushBlock(CSharpBlockKind.Enum);
GenerateDeclarationCommon(@enum);
if (@enum.IsFlags)
@ -1585,6 +1611,8 @@ namespace CppSharp.Generators.CSharp @@ -1585,6 +1611,8 @@ namespace CppSharp.Generators.CSharp
NewLine();
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
public static string GetOperatorIdentifier(CXXOperatorKind kind,
@ -1732,6 +1760,7 @@ namespace CppSharp.Generators.CSharp @@ -1732,6 +1760,7 @@ namespace CppSharp.Generators.CSharp
if (!function.IsProcessed || function.ExplicityIgnored)
return;
PushBlock(CSharpBlockKind.InternalsClassMethod);
GenerateDeclarationCommon(function);
WriteLine("[SuppressUnmanagedCodeSecurity]");
@ -1800,7 +1829,7 @@ namespace CppSharp.Generators.CSharp @@ -1800,7 +1829,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("public static extern {0} {1}({2});", retType,
GetFunctionIdentifier(function),
string.Join(", ", @params));
NeedNewLine();
PopBlock(NewLineKind.BeforeNextBlock);
typePrinter.PopContext();
}

Loading…
Cancel
Save