Biohazrd icon indicating copy to clipboard operation
Biohazrd copied to clipboard

MoveLooseDeclarationsIntoTypesTransformation does not elegantly handle a function in the global namespace which references a type in a namespace

Open PathogenDavid opened this issue 3 years ago • 0 comments

This was found in various places around PhysX. PxSetJointGlobalFrame is one such example.

MoveLooseDeclarationsIntoTypesTransformation considers namespaces moving loose declarations. This means a function ::PxSetJointGlobalFrame will not be moved into physx::PxJoint, which is sensible.

Unfortunately this creates weird ambiguities and unusable output. For instance, the output for PxSetJointGlobalFrame looks similar to the following:

// PxJoint.cs
namespace physx
{
    public struct PxJoint
    {
        // ...
    }
}

public unsafe static partial class PxJoint
{
    [DllImport("...")]
    public static extern void PxSetJointGlobalFrame(PxJoint* joint, PxVec3* wsAnchor, PxVec3* wsAxis);
}

Which causes an error because it thinks the joint parameter is referring to the static class containing the method.

The "proper" fix here is to have the type emit aware of the need to explicitly disambiguate the type reference, but even then this isn't really what the generator author wanted or expected.

I think a better strategy would either be:

  • Switch to the default being a class named Globals. (Which is what I've been gravitating towards for Biohazrd-generated libraries like InfectedWin32 anyway.)
  • Automatically rename the generated type if it conflicts with a type in another namespace.

As a workaround, developers can manually override the naming logic by passing a lambda to MoveLooseDeclarationsIntoTypesTransformation's constructor.

PathogenDavid avatar May 09 '21 04:05 PathogenDavid