Browse Source

Merge pull request #48 from ddobrev/more_compile_fixes

More compile fixes
pull/49/merge
João Matos 12 years ago
parent
commit
3320064603
  1. 2
      src/AST/Type.cs
  2. 12
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 16
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 8
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  5. 49
      src/Parser/Parser.cpp
  6. 14
      tests/Basic/Basic.cpp
  7. 4
      tests/Basic/Basic.h

2
src/AST/Type.cs

@ -282,6 +282,8 @@ namespace CppSharp.AST
// Argument types. // Argument types.
public List<Parameter> Parameters; public List<Parameter> Parameters;
public CallingConvention CallingConvention { get; set; }
public FunctionType() public FunctionType()
{ {
Parameters = new List<Parameter>(); Parameters = new List<Parameter>();

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

@ -59,6 +59,8 @@ namespace CppSharp.Generators.CSharp
Context.MarshalToManaged = this; Context.MarshalToManaged = this;
} }
public int VarSuffix { get; set; }
public static string QualifiedIdentifier(Declaration decl) public static string QualifiedIdentifier(Declaration decl)
{ {
var names = new List<string> { decl.Name }; var names = new List<string> { decl.Name };
@ -207,6 +209,8 @@ namespace CppSharp.Generators.CSharp
if (ctx.Kind == CSharpMarshalKind.NativeField) if (ctx.Kind == CSharpMarshalKind.NativeField)
{ {
string copy = Generator.GeneratedIdentifier("copy"); string copy = Generator.GeneratedIdentifier("copy");
if (VarSuffix > 0)
copy += VarSuffix;
Context.SupportBefore.WriteLine( Context.SupportBefore.WriteLine(
"var {0} = new global::System.IntPtr(&{1});", copy, instance); "var {0} = new global::System.IntPtr(&{1});", copy, instance);
instance = copy; instance = copy;
@ -215,6 +219,8 @@ namespace CppSharp.Generators.CSharp
if (@class.IsRefType) if (@class.IsRefType)
{ {
var instanceName = Generator.GeneratedIdentifier("instance"); var instanceName = Generator.GeneratedIdentifier("instance");
if (VarSuffix > 0)
instanceName += VarSuffix;
// 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});",
@ -344,9 +350,9 @@ namespace CppSharp.Generators.CSharp
var pointee = pointer.Pointee; var pointee = pointer.Pointee;
Type type = pointee.Desugar(); Type type = pointee.Desugar();
if ((type.IsPrimitiveType(PrimitiveType.Char) || if ((type.IsPrimitiveType(PrimitiveType.Char) ||
type.IsPrimitiveType(PrimitiveType.WideChar)) && type.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst) pointer.QualifiedPointee.Qualifiers.IsConst)
{ {
Context.Return.Write("Marshal.StringToHGlobalAnsi({0})", Context.Return.Write("Marshal.StringToHGlobalAnsi({0})",
Helpers.SafeIdentifier(Context.Parameter.Name)); Helpers.SafeIdentifier(Context.Parameter.Name));

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

@ -503,8 +503,9 @@ namespace CppSharp.Generators.CSharp
GenerateStructMarshalingFields(@base.Class); GenerateStructMarshalingFields(@base.Class);
} }
foreach (var field in @class.Fields) for (int i = 0; i < @class.Fields.Count; i++)
{ {
var field = @class.Fields[i];
if (ASTUtils.CheckIgnoreField(field)) continue; if (ASTUtils.CheckIgnoreField(field)) continue;
var nativeField = string.Format("{0}->{1}", var nativeField = string.Format("{0}->{1}",
@ -519,6 +520,7 @@ namespace CppSharp.Generators.CSharp
}; };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx); var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
marshal.VarSuffix = i;
field.Visit(marshal); field.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1438,7 +1440,7 @@ namespace CppSharp.Generators.CSharp
GenerateDeclarationCommon(function); GenerateDeclarationCommon(function);
var functionName = GetFunctionIdentifier(function); var functionName = GetFunctionIdentifier(function);
Write("public static {0} {1}(", function.ReturnType, functionName); Write("public static {0} {1}(", function.OriginalReturnType, functionName);
GenerateMethodParameters(function); GenerateMethodParameters(function);
WriteLine(")"); WriteLine(")");
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -1845,21 +1847,23 @@ namespace CppSharp.Generators.CSharp
GenerateDeclarationCommon(typedef); GenerateDeclarationCommon(typedef);
FunctionType function; FunctionType functionType;
TagType tag; TagType tag;
if (typedef.Type.IsPointerToPrimitiveType(PrimitiveType.Void) if (typedef.Type.IsPointerToPrimitiveType(PrimitiveType.Void)
|| typedef.Type.IsPointerTo<TagType>(out tag)) || typedef.Type.IsPointerTo(out tag))
{ {
PushBlock(CSharpBlockKind.Typedef); PushBlock(CSharpBlockKind.Typedef);
WriteLine("public class " + SafeIdentifier(typedef.Name) + @" { }"); WriteLine("public class " + SafeIdentifier(typedef.Name) + @" { }");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
else if (typedef.Type.IsPointerTo<FunctionType>(out function)) else if (typedef.Type.IsPointerTo(out functionType))
{ {
PushBlock(CSharpBlockKind.Typedef); PushBlock(CSharpBlockKind.Typedef);
WriteLine("[UnmanagedFunctionPointerAttribute(CallingConvention.{0})]",
Helpers.ToCSharpCallConv(functionType.CallingConvention));
WriteLine("public {0};", WriteLine("public {0};",
string.Format(TypePrinter.VisitDelegate(function).Type, string.Format(TypePrinter.VisitDelegate(functionType).Type,
SafeIdentifier(typedef.Name))); SafeIdentifier(typedef.Name)));
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }

8
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -140,11 +140,9 @@ namespace CppSharp.Generators.CSharp
{ {
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
if (pointee.IsPrimitiveType(PrimitiveType.Char) && return (pointee.IsPrimitiveType(PrimitiveType.Char) ||
pointer.QualifiedPointee.Qualifiers.IsConst) pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
return true; pointer.QualifiedPointee.Qualifiers.IsConst;
return false;
} }
public static bool IsConstCharString(QualifiedType qualType) public static bool IsConstCharString(QualifiedType qualType)

49
src/Parser/Parser.cpp

@ -1040,6 +1040,30 @@ clang::TypeLoc ResolveTypeLoc(clang::TypeLoc TL, clang::TypeLoc::TypeLocClass Cl
return TL; return TL;
} }
static CppSharp::AST::CallingConvention ConvertCallConv(clang::CallingConv CC)
{
using namespace clang;
switch(CC)
{
case CC_Default:
case CC_C:
return CppSharp::AST::CallingConvention::C;
case CC_X86StdCall:
return CppSharp::AST::CallingConvention::StdCall;
case CC_X86FastCall:
return CppSharp::AST::CallingConvention::FastCall;
case CC_X86ThisCall:
return CppSharp::AST::CallingConvention::ThisCall;
case CC_X86Pascal:
case CC_AAPCS:
case CC_AAPCS_VFP:
return CppSharp::AST::CallingConvention::Unknown;
}
return CppSharp::AST::CallingConvention::Default;
}
CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
bool DesugarType) bool DesugarType)
{ {
@ -1195,6 +1219,7 @@ CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc*
auto F = gcnew CppSharp::AST::FunctionType(); auto F = gcnew CppSharp::AST::FunctionType();
F->ReturnType = GetQualifiedType(FP->getResultType(), F->ReturnType = GetQualifiedType(FP->getResultType(),
WalkType(FP->getResultType(), &RL)); WalkType(FP->getResultType(), &RL));
F->CallingConvention = ConvertCallConv(FP->getCallConv());
for (unsigned i = 0; i < FP->getNumArgs(); ++i) for (unsigned i = 0; i < FP->getNumArgs(); ++i)
{ {
@ -1474,30 +1499,6 @@ clang::CallingConv Parser::GetAbiCallConv(clang::CallingConv CC,
return CC; return CC;
} }
static CppSharp::AST::CallingConvention ConvertCallConv(clang::CallingConv CC)
{
using namespace clang;
switch(CC)
{
case CC_Default:
case CC_C:
return CppSharp::AST::CallingConvention::C;
case CC_X86StdCall:
return CppSharp::AST::CallingConvention::StdCall;
case CC_X86FastCall:
return CppSharp::AST::CallingConvention::FastCall;
case CC_X86ThisCall:
return CppSharp::AST::CallingConvention::ThisCall;
case CC_X86Pascal:
case CC_AAPCS:
case CC_AAPCS_VFP:
return CppSharp::AST::CallingConvention::Unknown;
}
return CppSharp::AST::CallingConvention::Default;
}
static const clang::CodeGen::CGFunctionInfo& GetCodeGenFuntionInfo( static const clang::CodeGen::CGFunctionInfo& GetCodeGenFuntionInfo(
clang::CodeGen::CodeGenTypes* CodeGenTypes, clang::FunctionDecl* FD) clang::CodeGen::CodeGenTypes* CodeGenTypes, clang::FunctionDecl* FD)
{ {

14
tests/Basic/Basic.cpp

@ -71,15 +71,25 @@ int Hello::RetEnum(Enum e)
return (int)e; return (int)e;
} }
int unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int)) int unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int))
{ {
return ret.A; return ret.A;
} }
const wchar_t* wcharFunction(const wchar_t* constWideChar)
{
return constWideChar;
}
Bar operator+(const Bar& b1, const Bar& b2) Bar operator+(const Bar& b1, const Bar& b2)
{ {
Bar b; Bar b;
b.A = b1.A + b2.A; b.A = b1.A + b2.A;
b.B = b1.B + b2.B; b.B = b1.B + b2.B;
return b; return b;
} }
Bar indirectReturn()
{
return Bar();
}

4
tests/Basic/Basic.h

@ -73,4 +73,6 @@ public:
DLL_API Bar operator+(const Bar &, const Bar &); DLL_API Bar operator+(const Bar &, const Bar &);
int DLL_API unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int)); int DLL_API unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int));
DLL_API Bar indirectReturn();

Loading…
Cancel
Save