diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 0e0ed742..a6941b8e 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -482,14 +482,16 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) var cxxMethodDecl = functionDecl as CXXMethodDecl; var body = functionDecl.Body; + var hasBody = body is not null; - var isVirtual = (cxxMethodDecl != null) && cxxMethodDecl.IsVirtual; + var isCxxMethodDecl = cxxMethodDecl is not null; + var isVirtual = isCxxMethodDecl && cxxMethodDecl.IsVirtual; var escapedName = isVirtual ? PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl)) : EscapeAndStripName(name); var returnType = functionDecl.ReturnType; var returnTypeName = GetRemappedTypeName(functionDecl, cxxRecordDecl, returnType, out var nativeTypeName); - if ((isVirtual || (body is null)) && (returnTypeName == "bool")) + if ((isVirtual || !hasBody) && (returnTypeName == "bool")) { // bool is not blittable, so we shouldn't use it for P/Invoke signatures returnTypeName = "byte"; @@ -499,7 +501,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) var type = functionDecl.Type; var callingConventionName = GetCallingConvention(functionDecl, cxxRecordDecl, type); - var isDllImport = body is null && !isVirtual; + var isDllImport = !hasBody && !isVirtual; var entryPoint = ""; if (isDllImport) @@ -507,7 +509,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) entryPoint = functionDecl.IsExternC ? GetCursorName(functionDecl) : functionDecl.Handle.Mangling.CString; } - var needsReturnFixup = isVirtual && NeedsReturnFixup(cxxMethodDecl); + var needsReturnFixup = isCxxMethodDecl && NeedsReturnFixup(cxxMethodDecl); var desc = new FunctionOrDelegateDesc { AccessSpecifier = accessSppecifier, @@ -521,8 +523,8 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) IsDllImport = isDllImport, HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen, SetLastError = GetSetLastError(functionDecl), - IsCxx = cxxMethodDecl is not null, - IsStatic = isDllImport || (cxxMethodDecl?.IsStatic ?? true), + IsCxx = isCxxMethodDecl, + IsStatic = isDllImport || !isCxxMethodDecl || cxxMethodDecl.IsStatic, NeedsNewKeyword = NeedsNewKeyword(escapedName, functionDecl.Parameters), IsUnsafe = IsUnsafe(functionDecl), IsCtxCxxRecord = cxxRecordDecl is not null, @@ -531,7 +533,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) ReturnType = needsReturnFixup ? $"{returnTypeName}*" : returnTypeName, IsCxxConstructor = functionDecl is CXXConstructorDecl, Location = functionDecl.Location, - HasBody = body is not null, + HasBody = hasBody, WriteCustomAttrs = static context => { (var functionDecl, var outputBuilder, var generator) = ((FunctionDecl, IOutputBuilder, PInvokeGenerator))context; @@ -552,7 +554,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) _outputBuilder.BeginFunctionInnerPrototype(escapedName); - if (isVirtual) + if (isVirtual || (isCxxMethodDecl && !hasBody && cxxMethodDecl.IsInstance)) { Debug.Assert(cxxRecordDecl != null); @@ -593,7 +595,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) _outputBuilder.EndFunctionInnerPrototype(); - if ((body is not null) && !isVirtual) + if (hasBody && !isVirtual) { var firstCtorInitializer = functionDecl.Parameters.Any() ? (functionDecl.CursorChildren.IndexOf(functionDecl.Parameters.Last()) + 1) : 0; var lastCtorInitializer = (functionDecl.Body != null) ? functionDecl.CursorChildren.IndexOf(functionDecl.Body) : functionDecl.CursorChildren.Count; @@ -3406,6 +3408,26 @@ private bool IsConstant(string targetTypeName, Expr initExpr) case CX_StmtClass.CX_StmtClass_CallExpr: { + var callExpr = (CallExpr)initExpr; + + if (callExpr.DirectCallee.IsInlined) + { + var evaluateResult = callExpr.Handle.Evaluate; + + switch (evaluateResult.Kind) + { + case CXEvalResultKind.CXEval_Int: + { + return true; + } + + case CXEvalResultKind.CXEval_Float: + { + return true; + } + } + } + return false; } diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index 13cc1cfa..89677996 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Text; @@ -68,6 +69,51 @@ private void VisitCallExpr(CallExpr callExpr) var outputBuilder = StartCSharpCode(); var calleeDecl = callExpr.CalleeDecl; + if (callExpr.DirectCallee.IsInlined) + { + var evalResult = callExpr.Handle.Evaluate; + var canonicalType = callExpr.Type.CanonicalType; + + switch (evalResult.Kind) + { + case CXEvalResultKind.CXEval_Int: + { + if (canonicalType.Handle.IsUnsigned) + { + outputBuilder.Write(evalResult.AsUnsigned); + } + else + { + outputBuilder.Write(evalResult.AsLongLong); + } + + StopCSharpCode(); + return; + } + + case CXEvalResultKind.CXEval_Float: + { + if (canonicalType.Kind == CXTypeKind.CXType_Float) + { + outputBuilder.Write((float)evalResult.AsDouble); + } + else + { + outputBuilder.Write(evalResult.AsDouble); + } + + StopCSharpCode(); + return; + } + + case CXEvalResultKind.CXEval_StrLiteral: + { + AddDiagnostic(DiagnosticLevel.Info, "Possible string constant"); + break; + } + } + } + if (calleeDecl is null) { Visit(callExpr.Callee); diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/CXXMethodDeclarationTest.cs index 64497585..db8f8a54 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/CXXMethodDeclarationTest.cs @@ -185,7 +185,7 @@ namespace ClangSharp.Test public partial struct MyStruct {{ [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.ThisCall, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] - public static extern void MyVoidMethod(); + public static extern void MyVoidMethod(MyStruct* pThis); public int MyInt32Method() {{ diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/CXXMethodDeclarationTest.cs index 006b0276..8cb89e73 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/CXXMethodDeclarationTest.cs @@ -185,7 +185,7 @@ namespace ClangSharp.Test public partial struct MyStruct {{ [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.ThisCall, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] - public static extern void MyVoidMethod(); + public static extern void MyVoidMethod(MyStruct* pThis); public int MyInt32Method() {{ diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/CXXMethodDeclarationTest.cs index 3381e889..e2253f5c 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/CXXMethodDeclarationTest.cs @@ -185,7 +185,7 @@ namespace ClangSharp.Test public partial struct MyStruct {{ [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.ThisCall, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] - public static extern void MyVoidMethod(); + public static extern void MyVoidMethod(MyStruct* pThis); public int MyInt32Method() {{ diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs index 5ef9be14..8897525f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/CXXMethodDeclarationTest.cs @@ -185,7 +185,7 @@ namespace ClangSharp.Test public partial struct MyStruct {{ [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.ThisCall, EntryPoint = ""{entryPoint}"", ExactSpelling = true)] - public static extern void MyVoidMethod(); + public static extern void MyVoidMethod(MyStruct* pThis); public int MyInt32Method() {{ diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs index ebe87edf..bad94891 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/CXXMethodDeclarationTest.cs @@ -226,6 +226,9 @@ int MyInt32Method() void + + MyStruct* + int diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs index 9e78e670..4d6b8a11 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/CXXMethodDeclarationTest.cs @@ -226,6 +226,9 @@ int MyInt32Method() void + + MyStruct* + int diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs index d31d4632..8a6a4664 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/CXXMethodDeclarationTest.cs @@ -226,6 +226,9 @@ int MyInt32Method() void + + MyStruct* + int diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs index 77312ef2..578665bb 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/CXXMethodDeclarationTest.cs @@ -226,6 +226,9 @@ int MyInt32Method() void + + MyStruct* + int