Browse Source

Cleaned up the C# generator.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/502/head
Dimitar Dobrev 11 years ago
parent
commit
2e57ce7808
  1. 65
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

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

@ -19,7 +19,7 @@ namespace CppSharp.Generators.CSharp
public static class Helpers public static class Helpers
{ {
// from https://github.com/mono/mono/blob/master/mcs/class/System/Microsoft.CSharp/CSharpCodeGenerator.cs // from https://github.com/mono/mono/blob/master/mcs/class/System/Microsoft.CSharp/CSharpCodeGenerator.cs
private static readonly string[] Keywords = new string[] private static readonly string[] Keywords =
{ {
"abstract", "event", "new", "struct", "as", "explicit", "null", "switch", "abstract", "event", "new", "struct", "as", "explicit", "null", "switch",
"base", "extern", "this", "false", "operator", "throw", "break", "finally", "base", "extern", "this", "false", "operator", "throw", "break", "finally",
@ -484,14 +484,6 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateUnionFields(Class @class)
{
foreach (var field in @class.Fields)
{
GenerateClassField(field);
}
}
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
PushBlock(CSharpBlockKind.InternalsClass); PushBlock(CSharpBlockKind.InternalsClass);
@ -599,24 +591,20 @@ namespace CppSharp.Generators.CSharp
{ {
@params.Add("global::System.IntPtr instance"); @params.Add("global::System.IntPtr instance");
if (method.IsConstructor && base.Options.IsMicrosoftAbi) if (method.IsConstructor && Options.IsMicrosoftAbi)
retType = "global::System.IntPtr"; retType = "global::System.IntPtr";
} }
foreach (var param in function.Parameters) @params.AddRange(from param in function.Parameters
{ where param.Kind != ParameterKind.OperatorParameter
if (param.Kind == ParameterKind.OperatorParameter) let typeName = param.CSharpType(TypePrinter)
continue; select string.Format("{0} {1}", typeName, param.Name));
var typeName = param.CSharpType(TypePrinter);
@params.Add(string.Format("{0} {1}", typeName, param.Name));
}
if (method != null && method.IsConstructor) if (method != null && method.IsConstructor)
{ {
var @class = (Class) method.Namespace; var @class = (Class) method.Namespace;
if (Options.IsMicrosoftAbi && @class.Layout.HasVirtualBases) if (Options.IsMicrosoftAbi && @class.Layout.HasVirtualBases)
@params.Add("int " + CSharpTextTemplate.GeneratedIdentifier("forBases")); @params.Add("int " + GeneratedIdentifier("forBases"));
} }
TypePrinter.PopContext(); TypePrinter.PopContext();
@ -752,8 +740,6 @@ namespace CppSharp.Generators.CSharp
GenerateDeclarationCommon(field); GenerateDeclarationCommon(field);
var @class = (Class) field.Namespace;
WriteLine("{0} {1} {2};", @public ? "public" : "private", WriteLine("{0} {1} {2};", @public ? "public" : "private",
field.Type, field.Name); field.Type, field.Name);
@ -779,8 +765,7 @@ namespace CppSharp.Generators.CSharp
return Tuple.Create(library, decl.Mangled); return Tuple.Create(library, decl.Mangled);
} }
private void GeneratePropertySetter<T>(QualifiedType returnType, T decl, private void GeneratePropertySetter<T>(T decl, Class @class, bool isAbstract = false)
Class @class, bool isAbstract = false)
where T : Declaration, ITypedDecl where T : Declaration, ITypedDecl
{ {
if (!(decl is Function || decl is Field) ) if (!(decl is Function || decl is Field) )
@ -831,7 +816,7 @@ namespace CppSharp.Generators.CSharp
{ {
if (method.OperatorKind == CXXOperatorKind.Subscript) if (method.OperatorKind == CXXOperatorKind.Subscript)
{ {
GenerateIndexerSetter(returnType, method); GenerateIndexerSetter(method);
} }
else else
{ {
@ -917,7 +902,7 @@ namespace CppSharp.Generators.CSharp
return false; return false;
} }
private void GenerateIndexerSetter(QualifiedType returnType, Function function) private void GenerateIndexerSetter(Function function)
{ {
Type type; Type type;
function.Type.IsPointerTo(out type); function.Type.IsPointerTo(out type);
@ -1179,7 +1164,7 @@ namespace CppSharp.Generators.CSharp
GeneratePropertyGetter(prop.QualifiedType, prop.Field, @class); GeneratePropertyGetter(prop.QualifiedType, prop.Field, @class);
if (prop.HasSetter) if (prop.HasSetter)
GeneratePropertySetter(prop.Field.QualifiedType, prop.Field, @class); GeneratePropertySetter(prop.Field, @class);
} }
else else
{ {
@ -1187,7 +1172,7 @@ namespace CppSharp.Generators.CSharp
GeneratePropertyGetter(prop.QualifiedType, prop.GetMethod, @class, prop.IsPure); GeneratePropertyGetter(prop.QualifiedType, prop.GetMethod, @class, prop.IsPure);
if (prop.HasSetter) if (prop.HasSetter)
GeneratePropertySetter(prop.QualifiedType, prop.SetMethod, @class, prop.IsPure); GeneratePropertySetter(prop.SetMethod, @class, prop.IsPure);
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
@ -1212,7 +1197,7 @@ namespace CppSharp.Generators.CSharp
GeneratePropertyGetter(variable.QualifiedType, variable, @class); GeneratePropertyGetter(variable.QualifiedType, variable, @class);
if (!variable.QualifiedType.Qualifiers.IsConst) if (!variable.QualifiedType.Qualifiers.IsConst)
GeneratePropertySetter(variable.QualifiedType, variable, @class); GeneratePropertySetter(variable, @class);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -1966,7 +1951,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private bool GenerateClassConstructorBase(Class @class, Method method) private void GenerateClassConstructorBase(Class @class, Method method)
{ {
var hasBase = @class.HasBaseClass; var hasBase = @class.HasBaseClass;
@ -1975,10 +1960,7 @@ namespace CppSharp.Generators.CSharp
PushIndent(); PushIndent();
Write(": this("); Write(": this(");
if (method != null) Write(method != null ? "(Internal*) null" : "native");
Write("(Internal*) null");
else
Write("native");
WriteLine(")"); WriteLine(")");
PopIndent(); PopIndent();
@ -1986,8 +1968,6 @@ namespace CppSharp.Generators.CSharp
if (@class.IsValueType) if (@class.IsValueType)
WriteLineIndent(": this()"); WriteLineIndent(": this()");
return hasBase;
} }
#endregion #endregion
@ -2111,7 +2091,7 @@ namespace CppSharp.Generators.CSharp
} }
else if (method.IsOperator) else if (method.IsOperator)
{ {
GenerateOperator(method, @class); GenerateOperator(method);
} }
else if (method.SynthKind == FunctionSynthKind.AbstractImplCall) else if (method.SynthKind == FunctionSynthKind.AbstractImplCall)
{ {
@ -2130,7 +2110,7 @@ namespace CppSharp.Generators.CSharp
} }
else if (method.IsOperator) else if (method.IsOperator)
{ {
GenerateOperator(method, @class); GenerateOperator(method);
} }
else else
{ {
@ -2228,7 +2208,7 @@ namespace CppSharp.Generators.CSharp
return virtualCallBuilder.ToString(); return virtualCallBuilder.ToString();
} }
private void GenerateOperator(Method method, Class @class) private void GenerateOperator(Method method)
{ {
if (method.SynthKind == FunctionSynthKind.ComplementOperator) if (method.SynthKind == FunctionSynthKind.ComplementOperator)
{ {
@ -2396,7 +2376,7 @@ namespace CppSharp.Generators.CSharp
if (needsInstance) if (needsInstance)
{ {
var instanceIndex = GetInstanceParamIndex(function); var instanceIndex = GetInstanceParamIndex(method);
if (needsFixedThis) if (needsFixedThis)
{ {
@ -2480,10 +2460,8 @@ namespace CppSharp.Generators.CSharp
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
private int GetInstanceParamIndex(Function function) private int GetInstanceParamIndex(Method method)
{ {
var method = function as Method;
if (Options.IsMicrosoftAbi) if (Options.IsMicrosoftAbi)
return 0; return 0;
@ -2837,8 +2815,7 @@ namespace CppSharp.Generators.CSharp
if (method != null && method.IsConstructor) if (method != null && method.IsConstructor)
{ {
var @class = method.Namespace as Class; if (Options.IsMicrosoftAbi && ((Class) method.Namespace).Layout.HasVirtualBases)
if (Options.IsMicrosoftAbi && @class.Layout.HasVirtualBases)
@params.Add("int " + GeneratedIdentifier("forBases")); @params.Add("int " + GeneratedIdentifier("forBases"));
} }

Loading…
Cancel
Save