Browse Source

Refactored some function/method generator code to make it easier to re-use.

pull/1/head
triton 12 years ago
parent
commit
868515303b
  1. 64
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

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

@ -293,23 +293,18 @@ namespace CppSharp.Generators.CSharp
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]", WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]",
@class.Layout.Size); @class.Layout.Size);
Write("internal "); GenerateClassInternalHead(@class);
if (@class.HasBaseClass)
Write("new ");
WriteLine("struct Internal");
WriteStartBraceIndent(); WriteStartBraceIndent();
ResetNewLine(); ResetNewLine();
GenerateClassFields(@class, isInternal: true); var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
GenerateClassFields(@class, isInternal: true);
foreach (var ctor in @class.Constructors) foreach (var ctor in @class.Constructors)
{ {
@ -317,7 +312,7 @@ namespace CppSharp.Generators.CSharp
continue; continue;
NewLineIfNeeded(); NewLineIfNeeded();
GenerateFunction(ctor, @class); GenerateInternalFunction(ctor);
} }
foreach (var method in @class.Methods) foreach (var method in @class.Methods)
@ -332,13 +327,23 @@ namespace CppSharp.Generators.CSharp
continue; continue;
NewLineIfNeeded(); NewLineIfNeeded();
GenerateFunction(method, @class); GenerateInternalFunction(method);
} }
typePrinter.PopContext();
WriteCloseBraceIndent(); WriteCloseBraceIndent();
NeedNewLine(); NeedNewLine();
}
typePrinter.PopContext(); private void GenerateClassInternalHead(Class @class = null)
{
Write("public ");
if (@class != null && @class.HasBaseClass)
Write("new ");
WriteLine("struct Internal");
} }
private void GenerateStructMarshalingFields(Class @class) private void GenerateStructMarshalingFields(Class @class)
@ -603,7 +608,7 @@ namespace CppSharp.Generators.CSharp
{ {
var function = decl as Function; var function = decl as Function;
var parameters = new List<Parameter> { param }; var parameters = new List<Parameter> { param };
GenerateInternalFunctionCall(function, @class, parameters); GenerateInternalFunctionCall(function, parameters);
} }
else if (decl is Field) else if (decl is Field)
{ {
@ -638,7 +643,7 @@ namespace CppSharp.Generators.CSharp
if (decl is Function) if (decl is Function)
{ {
var function = decl as Function; var function = decl as Function;
GenerateInternalFunctionCall(function, @class); GenerateInternalFunctionCall(function);
@return = "ret"; @return = "ret";
} }
else if (decl is Field) else if (decl is Field)
@ -923,7 +928,7 @@ namespace CppSharp.Generators.CSharp
else else
Write("{0} {1}(", method.ReturnType, functionName); Write("{0} {1}(", method.ReturnType, functionName);
GenerateMethodParameters(method, @class); GenerateMethodParameters(method);
WriteLine(")"); WriteLine(")");
@ -944,14 +949,14 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
GenerateInternalFunctionCall(method, @class); GenerateInternalFunctionCall(method);
} }
} }
else if (@class.IsValueType) else if (@class.IsValueType)
{ {
if (method.IsConstructor) if (method.IsConstructor)
{ {
GenerateInternalFunctionCall(method, @class); GenerateInternalFunctionCall(method);
} }
else if (method.IsOperator) else if (method.IsOperator)
{ {
@ -959,7 +964,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
GenerateInternalFunctionCall(method, @class); GenerateInternalFunctionCall(method);
} }
} }
@ -1001,7 +1006,7 @@ namespace CppSharp.Generators.CSharp
return; return;
} }
GenerateInternalFunctionCall(method, @class); GenerateInternalFunctionCall(method);
} }
private void GenerateClassConstructor(Method method, Class @class) private void GenerateClassConstructor(Method method, Class @class)
@ -1018,7 +1023,7 @@ namespace CppSharp.Generators.CSharp
WriteLine(");"); WriteLine(");");
} }
public void GenerateInternalFunctionCall(Function function, Class @class, public void GenerateInternalFunctionCall(Function function,
List<Parameter> parameters = null) List<Parameter> parameters = null)
{ {
if (parameters == null) if (parameters == null)
@ -1026,7 +1031,7 @@ namespace CppSharp.Generators.CSharp
var functionName = string.Format("Internal.{0}", var functionName = string.Format("Internal.{0}",
GetFunctionNativeIdentifier(function)); GetFunctionNativeIdentifier(function));
GenerateFunctionCall(functionName, parameters, function, @class); GenerateFunctionCall(functionName, parameters, function);
} }
public void GenerateFunctionCall(string functionName, List<Parameter> parameters, public void GenerateFunctionCall(string functionName, List<Parameter> parameters,
@ -1265,13 +1270,13 @@ namespace CppSharp.Generators.CSharp
} }
} }
private void GenerateMethodParameters(Method method, Class @class) private void GenerateMethodParameters(Function function)
{ {
var @params = new List<string>(); var @params = new List<string>();
for (var i = 0; i < method.Parameters.Count; ++i) for (var i = 0; i < function.Parameters.Count; ++i)
{ {
var param = method.Parameters[i]; var param = function.Parameters[i];
if (param.Kind == ParameterKind.HiddenStructureReturn) if (param.Kind == ParameterKind.HiddenStructureReturn)
continue; continue;
@ -1491,7 +1496,7 @@ namespace CppSharp.Generators.CSharp
return true; return true;
} }
public void GenerateFunction(Function function, Class @class = null) public void GenerateInternalFunction(Function function)
{ {
if(function.Ignore) return; if(function.Ignore) return;
@ -1515,6 +1520,8 @@ namespace CppSharp.Generators.CSharp
var @params = new List<string>(); var @params = new List<string>();
var typePrinter = TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
var retType = typePrinter.VisitParameterDecl(new Parameter() var retType = typePrinter.VisitParameterDecl(new Parameter()
{ {
QualifiedType = function.ReturnType QualifiedType = function.ReturnType
@ -1545,13 +1552,18 @@ namespace CppSharp.Generators.CSharp
} }
if (method != null && method.IsConstructor) if (method != null && method.IsConstructor)
{
var @class = method.Namespace as Class;
if (Options.IsMicrosoftAbi && @class.Layout.HasVirtualBases) if (Options.IsMicrosoftAbi && @class.Layout.HasVirtualBases)
@params.Add("int " + GeneratedIdentifier("forBases")); @params.Add("int " + GeneratedIdentifier("forBases"));
}
WriteLine("public unsafe static extern {0} {1}({2});", retType, WriteLine("public unsafe static extern {0} {1}({2});", retType,
GetFunctionIdentifier(function), GetFunctionIdentifier(function),
string.Join(", ", @params)); string.Join(", ", @params));
NeedNewLine(); NeedNewLine();
typePrinter.PopContext();
} }
} }

Loading…
Cancel
Save