ClangSharp icon indicating copy to clipboard operation
ClangSharp copied to clipboard

Fix imported constructors and instance methods

Open hermann-noll opened this issue 4 years ago • 0 comments

Constructors were supported when they had bodies and were generated as actual C# constructors, but trying to import them produced invalid C#, e.g:

    public partial struct MyStruct
    {
        [DllImport("", CallingConvention = CallingConvention.ThisCall, EntryPoint = "...", ExactSpelling = true)]
        public static extern MyStruct();
    }

So three things had to be fixed:

  • The name has to be remapped to not conflict with C# constructors (static or not)
    Also copy/move constructors will conflict with each other thus they get different names
  • A return type has to be added (Clang said void although it might also be a pointer to the struct?)
  • A pThis parameter has to be added.
    This point applied to all imported instance methods.

Here is an example of the result of this PR:

    public partial struct MyStruct
    {
        [DllImport("", CallingConvention = CallingConvention.ThisCall, EntryPoint = "...", ExactSpelling = true)]
        public static extern unsafe void Constructor(MyStruct* pThis);

        [DllImport("", CallingConvention = CallingConvention.ThisCall, EntryPoint = "...", ExactSpelling = true)]
        public static extern unsafe void CopyConstructor(MyStruct* pThis, [NativeTypeName("const MyStruct &")] MyStruct* other);

        [DllImport("", CallingConvention = CallingConvention.ThisCall, EntryPoint = "...", ExactSpelling=true)]
        public static extern unsafe void MoveConstructor(MyStruct* pThis, [NativeTypeName("MyStruct &&")] MyStruct* other);

        [DllImport("", CallingConvention = CallingConvention.ThisCall, EntryPoint = "...", ExactSpelling = true)]
        public static extern unsafe void MyMethod(MyStruct* pThis);
    }

hermann-noll avatar Aug 30 '21 08:08 hermann-noll