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. 6
      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. 10
      tests/Basic/Basic.cpp
  7. 2
      tests/Basic/Basic.h

2
src/AST/Type.cs

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

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

@ -59,6 +59,8 @@ namespace CppSharp.Generators.CSharp @@ -59,6 +59,8 @@ namespace CppSharp.Generators.CSharp
Context.MarshalToManaged = this;
}
public int VarSuffix { get; set; }
public static string QualifiedIdentifier(Declaration decl)
{
var names = new List<string> { decl.Name };
@ -207,6 +209,8 @@ namespace CppSharp.Generators.CSharp @@ -207,6 +209,8 @@ namespace CppSharp.Generators.CSharp
if (ctx.Kind == CSharpMarshalKind.NativeField)
{
string copy = Generator.GeneratedIdentifier("copy");
if (VarSuffix > 0)
copy += VarSuffix;
Context.SupportBefore.WriteLine(
"var {0} = new global::System.IntPtr(&{1});", copy, instance);
instance = copy;
@ -215,6 +219,8 @@ namespace CppSharp.Generators.CSharp @@ -215,6 +219,8 @@ namespace CppSharp.Generators.CSharp
if (@class.IsRefType)
{
var instanceName = Generator.GeneratedIdentifier("instance");
if (VarSuffix > 0)
instanceName += VarSuffix;
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",

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

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

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

@ -140,11 +140,9 @@ namespace CppSharp.Generators.CSharp @@ -140,11 +140,9 @@ namespace CppSharp.Generators.CSharp
{
var pointee = pointer.Pointee.Desugar();
if (pointee.IsPrimitiveType(PrimitiveType.Char) &&
pointer.QualifiedPointee.Qualifiers.IsConst)
return true;
return false;
return (pointee.IsPrimitiveType(PrimitiveType.Char) ||
pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst;
}
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 @@ -1040,6 +1040,30 @@ clang::TypeLoc ResolveTypeLoc(clang::TypeLoc TL, clang::TypeLoc::TypeLocClass Cl
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,
bool DesugarType)
{
@ -1195,6 +1219,7 @@ CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* @@ -1195,6 +1219,7 @@ CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc*
auto F = gcnew CppSharp::AST::FunctionType();
F->ReturnType = GetQualifiedType(FP->getResultType(),
WalkType(FP->getResultType(), &RL));
F->CallingConvention = ConvertCallConv(FP->getCallConv());
for (unsigned i = 0; i < FP->getNumArgs(); ++i)
{
@ -1474,30 +1499,6 @@ clang::CallingConv Parser::GetAbiCallConv(clang::CallingConv CC, @@ -1474,30 +1499,6 @@ clang::CallingConv Parser::GetAbiCallConv(clang::CallingConv 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(
clang::CodeGen::CodeGenTypes* CodeGenTypes, clang::FunctionDecl* FD)
{

10
tests/Basic/Basic.cpp

@ -76,6 +76,11 @@ int unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int)) @@ -76,6 +76,11 @@ int unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int))
return ret.A;
}
const wchar_t* wcharFunction(const wchar_t* constWideChar)
{
return constWideChar;
}
Bar operator+(const Bar& b1, const Bar& b2)
{
Bar b;
@ -83,3 +88,8 @@ Bar operator+(const Bar& b1, const Bar& b2) @@ -83,3 +88,8 @@ Bar operator+(const Bar& b1, const Bar& b2)
b.B = b1.B + b2.B;
return b;
}
Bar indirectReturn()
{
return Bar();
}

2
tests/Basic/Basic.h

@ -74,3 +74,5 @@ public: @@ -74,3 +74,5 @@ public:
DLL_API Bar operator+(const Bar &, const Bar &);
int DLL_API unsafeFunction(const Bar& ret, char* testForString, void (*foo)(int));
DLL_API Bar indirectReturn();

Loading…
Cancel
Save