ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Generating nested types are not sufficiently qualified

Open Helco opened this issue 1 year ago • 1 comments

When using nested types as method parameters, the type is not fully declared leading to syntax errors in the C# code. A reproduction is as follows:

struct A
{
    struct Inner {};
};
struct B
{
    virtual void Method(A::Inner& inner);
};
USER@PC MINGW64 /c/Develop/prv/
$ ClangSharpPInvokeGenerator.exe --file bug/bug.h --output bug/output -n Bug
Processing 'bug/bug.h'

USER@PC MINGW64 /c/Develop/prv/
$ ClangSharpPInvokeGenerator --version
ClangSharp P/Invoke Binding Generator version 18.1.3
  clang version 18.1.3 (https://github.com/llvm/llvm-project c13b7485b87909fcf739f62cfa382b55407433c0)
  clangsharp version 18.1.3

leads to

namespace Bug
{
    public partial struct A
    {

        public partial struct Inner
        {
        }
    }
    public unsafe partial struct B
    {
        public void** lpVtbl;

        public void Method([NativeTypeName("A::Inner &")] Inner* inner)
        {
            ((delegate* unmanaged[Thiscall]<B*, Inner*, void>)(lpVtbl[0]))((B*)Unsafe.AsPointer(ref this), inner);
        }
    }
}

where Method should be declared as public void Method([NativeTypeName("A::Inner &")] A.Inner* inner).

A possible workaround is to use --remap A::Inner=AInner and to add an own C# file with the contents global using AInner = Bug.A.AInner;

Helco avatar Nov 18 '24 13:11 Helco

This shouldn't be difficult to fix, there's likely just a context being passed down incorrectly somewhere.

C++ bindings in general, especially as you get into more complex type definitions, are tricky and Clang doesn't always consistently surface the AST information in a way that makes it trivial to resolve.

tannergooding avatar Nov 18 '24 19:11 tannergooding