diff --git a/src/AST/Type.cs b/src/AST/Type.cs index c7ca5f98..84b78663 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -282,6 +282,8 @@ namespace CppSharp.AST // Argument types. public List Parameters; + public CallingConvention CallingConvention { get; set; } + public FunctionType() { Parameters = new List(); diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index cf0435c4..9b0e70ff 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -1847,7 +1847,7 @@ namespace CppSharp.Generators.CSharp GenerateDeclarationCommon(typedef); - FunctionType function; + FunctionType functionType; TagType tag; if (typedef.Type.IsPointerToPrimitiveType(PrimitiveType.Void) @@ -1857,12 +1857,13 @@ namespace CppSharp.Generators.CSharp WriteLine("public class " + SafeIdentifier(typedef.Name) + @" { }"); PopBlock(NewLineKind.BeforeNextBlock); } - else if (typedef.Type.IsPointerTo(out function)) + else if (typedef.Type.IsPointerTo(out functionType)) { PushBlock(CSharpBlockKind.Typedef); - WriteLine("[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]"); + 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); } diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index eeaa37ae..f70036fe 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -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* 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, 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) {