Browse Source

Merge pull request #46 from ddobrev/master

Compilation fixes
pull/47/merge
João Matos 12 years ago
parent
commit
52d269f6e4
  1. 2
      src/AST/Class.cs
  2. 2
      src/Generator/Generators/CLI/CLIMarshal.cs
  3. 7
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  4. 15
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  5. 62
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  6. 5
      src/Generator/Generators/Generator.cs
  7. 3
      src/Generator/Passes/CheckOperatorsOverloads.cs
  8. 5
      tests/Basic/Basic.cpp
  9. 11
      tests/Basic/Basic.h

2
src/AST/Class.cs

@ -145,7 +145,7 @@ namespace CppSharp.AST
public bool IsRefType public bool IsRefType
{ {
get { return Type == ClassType.RefType; } get { return Type == ClassType.RefType && !IsUnion; }
} }
public IEnumerable<Method> Constructors public IEnumerable<Method> Constructors

2
src/Generator/Generators/CLI/CLIMarshal.cs

@ -180,7 +180,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsRefType) if (@class.IsRefType)
{ {
var name = Helpers.GeneratedIdentifier(Context.ReturnVarName); var name = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name, Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name,
@class.QualifiedOriginalName, Context.ReturnVarName); @class.QualifiedOriginalName, Context.ReturnVarName);
instance = name; instance = name;

7
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -740,7 +740,8 @@ namespace CppSharp.Generators.CLI
var @params = GenerateFunctionParamsMarshal(function.Parameters, function); var @params = GenerateFunctionParamsMarshal(function.Parameters, function);
if (needsReturn) if (needsReturn)
Write("auto {0}ret = ",(function.ReturnType.Type.IsReference())? "&": string.Empty); Write("auto {0}{1} = ",(function.ReturnType.Type.IsReference())? "&": string.Empty,
Generator.GeneratedIdentifier("ret"));
if (!IsNativeFunctionOrStaticMethod(function)) if (!IsNativeFunctionOrStaticMethod(function))
{ {
@ -799,8 +800,8 @@ namespace CppSharp.Generators.CLI
{ {
var ctx = new MarshalContext(Driver) var ctx = new MarshalContext(Driver)
{ {
ArgName = "ret", ArgName = Generator.GeneratedIdentifier("ret"),
ReturnVarName = "ret", ReturnVarName = Generator.GeneratedIdentifier("ret"),
ReturnType = retType ReturnType = retType
}; };

15
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -185,7 +185,7 @@ namespace CppSharp.Generators.CSharp
FunctionType function; FunctionType function;
if (decl.Type.IsPointerTo(out function)) if (decl.Type.IsPointerTo(out function))
{ {
var ptrName = Helpers.GeneratedIdentifier("ptr") + var ptrName = Generator.GeneratedIdentifier("ptr") +
Context.ParameterIndex; Context.ParameterIndex;
Context.SupportBefore.WriteLine("var {0} = {1};", ptrName, Context.SupportBefore.WriteLine("var {0} = {1};", ptrName,
@ -206,12 +206,15 @@ namespace CppSharp.Generators.CSharp
string instance = Context.ReturnVarName; string instance = Context.ReturnVarName;
if (ctx.Kind == CSharpMarshalKind.NativeField) if (ctx.Kind == CSharpMarshalKind.NativeField)
{ {
instance = string.Format("new global::System.IntPtr(&{0})", instance); string copy = Generator.GeneratedIdentifier("copy");
Context.SupportBefore.WriteLine(
"var {0} = new global::System.IntPtr(&{1});", copy, instance);
instance = copy;
} }
if (@class.IsRefType) if (@class.IsRefType)
{ {
var instanceName = Helpers.GeneratedIdentifier("instance"); var instanceName = Generator.GeneratedIdentifier("instance");
// Allocate memory for a new native object and call the ctor. // Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});", Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
@ -363,17 +366,17 @@ namespace CppSharp.Generators.CSharp
if (Context.Parameter.Usage == ParameterUsage.Out) if (Context.Parameter.Usage == ParameterUsage.Out)
{ {
Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();", Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();",
Helpers.GeneratedIdentifier(Context.ArgName), @class.Name); Generator.GeneratedIdentifier(Context.ArgName), @class.Name);
} }
else else
{ {
Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();", Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();",
Helpers.GeneratedIdentifier(Context.ArgName), Generator.GeneratedIdentifier(Context.ArgName),
Helpers.SafeIdentifier(Context.Parameter.Name)); Helpers.SafeIdentifier(Context.Parameter.Name));
} }
Context.Return.Write("new global::System.IntPtr(&{0})", Context.Return.Write("new global::System.IntPtr(&{0})",
Helpers.GeneratedIdentifier(Context.ArgName)); Generator.GeneratedIdentifier(Context.ArgName));
return true; return true;
} }

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

@ -26,11 +26,6 @@ namespace CppSharp.Generators.CSharp
"void", "partial", "yield", "where" "void", "partial", "yield", "where"
}; };
public static string GeneratedIdentifier(string id)
{
return "_" + id;
}
public static string SafeIdentifier(string id) public static string SafeIdentifier(string id)
{ {
id = new string(((IEnumerable<char>)id) id = new string(((IEnumerable<char>)id)
@ -59,7 +54,7 @@ namespace CppSharp.Generators.CSharp
public static string InstanceIdentifier public static string InstanceIdentifier
{ {
get { return GeneratedIdentifier("Instance"); } get { return Generator.GeneratedIdentifier("Instance"); }
} }
} }
@ -124,7 +119,7 @@ namespace CppSharp.Generators.CSharp
public static string GeneratedIdentifier(string id) public static string GeneratedIdentifier(string id)
{ {
return Helpers.GeneratedIdentifier(id); return Generator.GeneratedIdentifier(id);
} }
public static string SafeIdentifier(string id) public static string SafeIdentifier(string id)
@ -216,7 +211,7 @@ namespace CppSharp.Generators.CSharp
if (context.HasFunctions) if (context.HasFunctions)
{ {
PushBlock(CSharpBlockKind.Functions); PushBlock(CSharpBlockKind.Functions);
WriteLine("public partial class {0}{1}", SafeIdentifier(Options.OutputNamespace), WriteLine("public unsafe partial class {0}{1}", SafeIdentifier(Options.OutputNamespace),
TranslationUnit.FileNameWithoutExtension); TranslationUnit.FileNameWithoutExtension);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -324,8 +319,8 @@ namespace CppSharp.Generators.CSharp
if (ShouldGenerateClassNativeField(@class)) if (ShouldGenerateClassNativeField(@class))
{ {
PushBlock(CSharpBlockKind.Field); PushBlock(CSharpBlockKind.Field);
WriteLine("public global::System.IntPtr {0} {{ get; protected set; }}", WriteLine("public global::System.IntPtr {0} {{ get; {1} set; }}",
Helpers.InstanceIdentifier); Helpers.InstanceIdentifier, @class.IsValueType ? "private" : "protected");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -513,7 +508,7 @@ namespace CppSharp.Generators.CSharp
if (ASTUtils.CheckIgnoreField(field)) continue; if (ASTUtils.CheckIgnoreField(field)) continue;
var nativeField = string.Format("{0}->{1}", var nativeField = string.Format("{0}->{1}",
Helpers.GeneratedIdentifier("ptr"), field.OriginalName); Generator.GeneratedIdentifier("ptr"), field.OriginalName);
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
@ -535,9 +530,9 @@ namespace CppSharp.Generators.CSharp
private void GenerateStructInternalMarshaling(Class @class) private void GenerateStructInternalMarshaling(Class @class)
{ {
var marshalVar = Helpers.GeneratedIdentifier("native"); var marshalVar = Generator.GeneratedIdentifier("native");
WriteLine("var {0} = new {1}.Internal();", marshalVar, @class.Name); WriteLine("var {0} = new {1}.Internal();", marshalVar, QualifiedIdentifier(@class));
GenerateStructInternalMarshalingFields(@class, marshalVar); GenerateStructInternalMarshalingFields(@class, marshalVar);
WriteLine("return {0};", marshalVar); WriteLine("return {0};", marshalVar);
@ -746,7 +741,7 @@ namespace CppSharp.Generators.CSharp
var field = decl as Field; var field = decl as Field;
WriteLine("var {0} = (Internal*){1}.ToPointer();", WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
param.Visit(marshal); param.Visit(marshal);
@ -754,7 +749,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
Write("{0}->{1} = {2}", Helpers.GeneratedIdentifier("ptr"), Write("{0}->{1} = {2}", Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(field.OriginalName), marshal.Context.Return); Helpers.SafeIdentifier(field.OriginalName), marshal.Context.Return);
WriteLine(";"); WriteLine(";");
@ -781,13 +776,13 @@ namespace CppSharp.Generators.CSharp
var field = decl as Field; var field = decl as Field;
WriteLine("var {0} = (Internal*){1}.ToPointer();", WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
Kind = CSharpMarshalKind.NativeField, Kind = CSharpMarshalKind.NativeField,
ArgName = decl.Name, ArgName = decl.Name,
ReturnVarName = string.Format("{0}->{1}", Helpers.GeneratedIdentifier("ptr"), ReturnVarName = string.Format("{0}->{1}", Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(field.OriginalName)), Helpers.SafeIdentifier(field.OriginalName)),
ReturnType = decl.QualifiedType ReturnType = decl.QualifiedType
}; };
@ -810,7 +805,7 @@ namespace CppSharp.Generators.CSharp
var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")", var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")",
libSymbol.Item1, libSymbol.Item2); libSymbol.Item1, libSymbol.Item2);
WriteLine("var {0} = ({1}*){2};", Helpers.GeneratedIdentifier("ptr"), WriteLine("var {0} = ({1}*){2};", Generator.GeneratedIdentifier("ptr"),
@var.Type, location); @var.Type, location);
TypePrinter.PopContext(); TypePrinter.PopContext();
@ -818,7 +813,7 @@ namespace CppSharp.Generators.CSharp
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
ArgName = decl.Name, ArgName = decl.Name,
ReturnVarName = "*" + Helpers.GeneratedIdentifier("ptr"), ReturnVarName = "*" + Generator.GeneratedIdentifier("ptr"),
ReturnType = new QualifiedType(var.Type) ReturnType = new QualifiedType(var.Type)
}; };
@ -1185,7 +1180,7 @@ namespace CppSharp.Generators.CSharp
var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true); var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true);
TypePrinter.PopContext(); TypePrinter.PopContext();
delegateInstance = Helpers.GeneratedIdentifier(@event.OriginalName); delegateInstance = Generator.GeneratedIdentifier(@event.OriginalName);
delegateName = delegateInstance + "Delegate"; delegateName = delegateInstance + "Delegate";
delegateRaise = delegateInstance + "RaiseInstance"; delegateRaise = delegateInstance + "RaiseInstance";
@ -1221,7 +1216,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name); WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name);
WriteLine("var {0} = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();", WriteLine("var {0} = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();",
Helpers.GeneratedIdentifier("ptr"), delegateInstance); Generator.GeneratedIdentifier("ptr"), delegateInstance);
// Call type map here. // Call type map here.
@ -1362,7 +1357,8 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}(global::System.IntPtr native)", SafeIdentifier(@class.Name)); WriteLine("internal {0}(global::System.IntPtr native){1}", SafeIdentifier(@class.Name),
@class.IsValueType ? " : this()" : string.Empty);
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType; var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
if (hasBaseClass) if (hasBaseClass)
@ -1376,13 +1372,13 @@ namespace CppSharp.Generators.CSharp
{ {
WriteLine("{0} = native;", Helpers.InstanceIdentifier); WriteLine("{0} = native;", Helpers.InstanceIdentifier);
if (Options.GenerateVirtualTables && @class.IsDynamic) if (Options.GenerateVirtualTables && @class.IsDynamic)
WriteLine("SetupVTables(_Instance);"); WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance"));
} }
} }
else else
{ {
WriteLine("var {0} = (Internal*){1}.ToPointer();", WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), "native"); Generator.GeneratedIdentifier("ptr"), "native");
GenerateStructMarshalingFields(@class); GenerateStructMarshalingFields(@class);
} }
@ -1401,7 +1397,7 @@ namespace CppSharp.Generators.CSharp
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
WriteLine("internal void FromInternal(Internal* native)"); WriteLine("internal void FromInternal(Internal* native)");
WriteStartBraceIndent(); WriteStartBraceIndent();
WriteLine("var {0} = {1};", Helpers.GeneratedIdentifier("ptr"), "native"); WriteLine("var {0} = {1};", Generator.GeneratedIdentifier("ptr"), "native");
GenerateStructMarshalingFields(@class); GenerateStructMarshalingFields(@class);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -1580,7 +1576,7 @@ namespace CppSharp.Generators.CSharp
GenerateFunctionParams(@params); GenerateFunctionParams(@params);
WriteLine(");"); WriteLine(");");
if (Options.GenerateVirtualTables && @class.IsDynamic) if (Options.GenerateVirtualTables && @class.IsDynamic)
WriteLine("SetupVTables(_Instance);"); WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance"));
} }
public void GenerateInternalFunctionCall(Function function, public void GenerateInternalFunctionCall(Function function,
@ -1636,7 +1632,7 @@ namespace CppSharp.Generators.CSharp
Class retClass = null; Class retClass = null;
hiddenParam.Type.Desugar().IsTagDecl(out retClass); hiddenParam.Type.Desugar().IsTagDecl(out retClass);
WriteLine("var ret = new {0}.Internal();", retClass.OriginalName); WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), QualifiedIdentifier(retClass));
} }
var names = new List<string>(); var names = new List<string>();
@ -1653,7 +1649,7 @@ namespace CppSharp.Generators.CSharp
if (function.HasHiddenStructParameter) if (function.HasHiddenStructParameter)
{ {
var name = string.Format("new IntPtr(&ret)"); var name = string.Format("new IntPtr(&{0})", GeneratedIdentifier("ret"));
names.Insert(0, name); names.Insert(0, name);
} }
@ -1668,11 +1664,11 @@ namespace CppSharp.Generators.CSharp
//WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName, //WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName,
// GeneratedIdentifier("instance")); // GeneratedIdentifier("instance"));
//WriteStartBraceIndent(); //WriteStartBraceIndent();
WriteLine("var {0} = ToInternal();", Helpers.GeneratedIdentifier("instance")); WriteLine("var {0} = ToInternal();", Generator.GeneratedIdentifier("instance"));
} }
if (needsReturn && !function.HasHiddenStructParameter) if (needsReturn && !function.HasHiddenStructParameter)
Write("var ret = "); Write("var {0} = ", GeneratedIdentifier("ret"));
WriteLine("{0}({1});", functionName, string.Join(", ", names)); WriteLine("{0}({1});", functionName, string.Join(", ", names));
@ -1696,15 +1692,15 @@ namespace CppSharp.Generators.CSharp
if (needsFixedThis) if (needsFixedThis)
{ {
// WriteCloseBraceIndent(); // WriteCloseBraceIndent();
WriteLine("FromInternal(&{0});", Helpers.GeneratedIdentifier("instance")); WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("instance"));
} }
if (needsReturn) if (needsReturn)
{ {
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
ArgName = "ret", ArgName = GeneratedIdentifier("ret"),
ReturnVarName = "ret", ReturnVarName = GeneratedIdentifier("ret"),
ReturnType = retType ReturnType = retType
}; };

5
src/Generator/Generators/Generator.cs

@ -98,5 +98,10 @@ namespace CppSharp.Generators
/// Generates the outputs for a given translation unit. /// Generates the outputs for a given translation unit.
/// </summary> /// </summary>
public abstract List<Template> Generate(TranslationUnit unit); public abstract List<Template> Generate(TranslationUnit unit);
public static string GeneratedIdentifier(string id)
{
return "__" + id;
}
} }
} }

3
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.CSharp; using CppSharp.Generators.CSharp;
namespace CppSharp.Passes namespace CppSharp.Passes
@ -64,7 +65,7 @@ namespace CppSharp.Passes
@operator.Parameters.Insert(0, new Parameter @operator.Parameters.Insert(0, new Parameter
{ {
Name = Helpers.GeneratedIdentifier("op"), Name = Generator.GeneratedIdentifier("op"),
QualifiedType = new QualifiedType(type), QualifiedType = new QualifiedType(type),
Kind = ParameterKind.OperatorParameter Kind = ParameterKind.OperatorParameter
}); });

5
tests/Basic/Basic.cpp

@ -70,3 +70,8 @@ int Hello::RetEnum(Enum e)
{ {
return (int)e; return (int)e;
} }
int unsafeFunction(const Bar& ret)
{
return ret.A;
}

11
tests/Basic/Basic.h

@ -15,6 +15,10 @@ public:
class DLL_API Foo2 : public Foo class DLL_API Foo2 : public Foo
{ {
struct Copy {
Foo A;
}* copy;
public: public:
int C; int C;
@ -42,6 +46,11 @@ enum Enum
class DLL_API Hello class DLL_API Hello
{ {
union {
int i;
float b;
};
public: public:
Hello (); Hello ();
@ -61,3 +70,5 @@ public:
int RetEnum(Enum); int RetEnum(Enum);
}; };
int DLL_API unsafeFunction(const Bar& ret);

Loading…
Cancel
Save