Browse Source

Bunch of improvements and fixes in the generation of value types.

pull/1/head
triton 12 years ago
parent
commit
7fb9e9438b
  1. 61
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

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

@ -833,19 +833,23 @@ namespace Cxxi.Generators.CSharp
} }
else else
{ {
GenerateFunctionCall(method, @class); GenerateInternalFunctionCall(method, @class);
} }
} }
else if (@class.IsValueType) else if (@class.IsValueType)
{ {
if (method.Kind != CXXMethodKind.Constructor) if (method.IsConstructor)
GenerateFunctionCall(method, @class); {
GenerateInternalFunctionCall(method, @class);
}
else if (method.IsOperator) else if (method.IsOperator)
{ {
GeneratedOperator(method, @class); GeneratedOperator(method, @class);
} }
else else
GenerateValueTypeConstructorCall(method, @class); {
GenerateInternalFunctionCall(method, @class);
}
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
@ -925,7 +929,7 @@ namespace Cxxi.Generators.CSharp
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("instance"), WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("instance"),
@class.QualifiedName); @class.QualifiedName);
GenerateFunctionCall(method, @class); GenerateInternalFunctionCall(method, @class);
GenerateValueTypeConstructorCallFields(@class); GenerateValueTypeConstructorCallFields(@class);
} }
@ -964,20 +968,28 @@ namespace Cxxi.Generators.CSharp
} }
} }
public void GenerateFunctionCall(Function function, Class @class = null) public void GenerateInternalFunctionCall(Function function, Class @class)
{
var functionName = string.Format("Internal.{0}",
GetFunctionNativeIdentifier(function, @class));
GenerateFunctionCall(functionName, function, @class);
}
public void GenerateFunctionCall(string functionName,
Function function, Class @class = null)
{ {
var retType = function.ReturnType; var retType = function.ReturnType;
var needsReturn = !retType.IsPrimitiveType(PrimitiveType.Void); var needsReturn = !retType.IsPrimitiveType(PrimitiveType.Void);
var method = function as Method;
var needsInstance = method != null && !method.IsStatic && !method.IsOperator;
var isValueType = @class != null && @class.IsValueType; var isValueType = @class != null && @class.IsValueType;
//if (isValueType) var needsFixedThis = needsInstance && isValueType;
//{
// WriteLine("auto this0 = (::{0}*) 0;", @class.QualifiedOriginalName);
//}
Class retClass = null;
if (function.HasHiddenStructParameter) if (function.HasHiddenStructParameter)
{ {
Class retClass;
function.ReturnType.IsTagDecl(out retClass); function.ReturnType.IsTagDecl(out retClass);
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("udt"), WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("udt"),
@ -999,16 +1011,23 @@ namespace Cxxi.Generators.CSharp
names.Insert(0, name); names.Insert(0, name);
} }
var method = function as Method; if (needsInstance)
{
names.Insert(0, needsFixedThis ? string.Format("new System.IntPtr({0})",
GeneratedIdentifier("instance")) : "Instance");
}
if (method != null && !method.IsStatic) if (needsFixedThis)
names.Insert(0, "Instance"); {
WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName,
GeneratedIdentifier("instance"));
WriteStartBraceIndent();
}
if (needsReturn) if (needsReturn)
Write("var ret = "); Write("var ret = ");
WriteLine("Internal.{0}({1});", GetFunctionNativeIdentifier(function, @class), WriteLine("{0}({1});", functionName, string.Join(", ", names));
string.Join(", ", names));
var cleanups = new List<TextGenerator>(); var cleanups = new List<TextGenerator>();
GenerateFunctionCallOutParams(@params, cleanups); GenerateFunctionCallOutParams(@params, cleanups);
@ -1047,13 +1066,10 @@ namespace Cxxi.Generators.CSharp
if (function.HasHiddenStructParameter) if (function.HasHiddenStructParameter)
{ {
Class retClass;
function.ReturnType.IsTagDecl(out retClass);
WriteLine("var ret = new {0}();", retClass.Name); WriteLine("var ret = new {0}();", retClass.Name);
if (isValueType) if (retClass.IsValueType)
WriteLine("*({0}.Internal*) ret = {1};", retClass.Name, WriteLine("*({0}.Internal*) &ret = {1};", retClass.Name,
GeneratedIdentifier("udt")); GeneratedIdentifier("udt"));
else else
WriteLine("*({0}.Internal*) ret.Instance.ToPointer() = {1};", WriteLine("*({0}.Internal*) ret.Instance.ToPointer() = {1};",
@ -1061,6 +1077,9 @@ namespace Cxxi.Generators.CSharp
WriteLine("return ret;"); WriteLine("return ret;");
} }
if (needsFixedThis)
WriteCloseBraceIndent();
} }
private void GenerateFunctionCallOutParams(IEnumerable<ParamMarshal> @params, private void GenerateFunctionCallOutParams(IEnumerable<ParamMarshal> @params,

Loading…
Cancel
Save