Example #1
0
    protected override TransformationResult TransformTypedef(TransformationContext context, TranslatedTypedef declaration)
    {
        if (declaration.File != PxBatchQueryDescFile)
        {
            return(declaration);
        }

        string?hitType = declaration.Name switch
        {
            "PxRaycastQueryResult" => "PxRaycastHit",
            "PxSweepQueryResult" => "PxSweepHit",
            "PxOverlapQueryResult" => "PxOverlapHit",
            _ => null
        };

        if (hitType is null)
        {
            return(declaration);
        }

        Debug.Assert(declaration.Namespace == "physx");
        Debug.Assert(declaration.UnderlyingType is TranslatedTypeReference);
        Debug.Assert(((TranslatedTypeReference)declaration.UnderlyingType).TryResolve(context.Library) is TranslatedUnsupportedDeclaration or null);

        return(declaration with
        {
            UnderlyingType = new ExternallyDefinedTypeReference("Mochi.PhysX", $"PxBatchQueryResult<{hitType}>")
        });
    }
}
Example #2
0
 protected override TransformationResult TransformTypedef(TransformationContext context, TranslatedTypedef declaration)
 {
     // ImWchar16 is defined as being ushort, but it makes more sense for it to be a C# char.
     if (declaration.Name is "ImWchar16")
     {
         Debug.Assert(declaration.UnderlyingType == CSharpBuiltinType.UShort);
         return(declaration with
         {
             UnderlyingType = CSharpBuiltinType.Char
         });
     }
     // ImWchar32 is defined as being uint, but it makes more sense for it to be a .NET System.Rune.
     // (In theory System.Rune is slightly different since it should only be used with valid unicode characters, but I think this is probably fine.)
     else if (declaration.Name is "ImWchar32")
     {
         Debug.Assert(declaration.UnderlyingType == CSharpBuiltinType.UInt);
         return(declaration with
         {
             UnderlyingType = new ExternallyDefinedTypeReference("System.Text", "Rune")
         });
     }
     else
     {
         return(declaration);
     }
 }
Example #3
0
    public InheritanceViaGenericAdapter(TranslatedLibrary library, Adapter targetAdapter, Adapter replacedAdapter)
        : base(targetAdapter)
    {
        // Save and validate our output type
        // (We mostly only validate the replaced adapter since we assume it's well-formed to adapt to this output.)
        OutputType = targetAdapter.InputType;

        if (OutputType is not PointerTypeReference)
        {
            throw new ArgumentException("The target adapter must take a pointer.", nameof(targetAdapter));
        }

        // Determine the actual record type
        TypeReference recordTypeReference = replacedAdapter.InputType;

        if (recordTypeReference is ByRefTypeReference byRefType)
        {
            ByRefKind           = byRefType.Kind;
            recordTypeReference = byRefType.Inner;
        }

        int pointerArity = 0;

        while (recordTypeReference is PointerTypeReference pointerType)
        {
            pointerArity++;
            recordTypeReference = pointerType.Inner;
        }

        if (ByRefKind is null && pointerArity == 0)
        {
            throw new ArgumentException("The replaced adapter must be passed by reference.", nameof(replacedAdapter));
        }

        if (recordTypeReference is not TranslatedTypeReference translatedType)
        {
            throw new ArgumentException("The replaced adapter does not adapt a translated type reference or uses an unrecognized method to do so.", nameof(replacedAdapter));
        }

        if (translatedType.TryResolve(library) is not TranslatedRecord targetRecord)
        {
            throw new ArgumentException("The replaced adapter does not adapt a record type reference.", nameof(replacedAdapter));
        }

        // Determine the constraint info
        ConstraintInterfaceNamespace = $"{targetRecord.Namespace}.{PhysXMarkerInterfacesDeclaration.InfrastructureNamespaceName}";
        ConstraintInterfaceTypeName  = $"I{targetRecord.Name}";

        // Name the generic parameter based on the parameter name
        GenericParameterName = $"T{Char.ToUpperInvariant(replacedAdapter.Name[0])}{replacedAdapter.Name.AsSpan().Slice(1)}";

        // Name our temporary (only used when we're byref)
        TemporaryName = $"__{Name}P";

        // Create the input type
        InputType = new ExternallyDefinedTypeReference(GenericParameterName);

        for (int i = 0; i < pointerArity; i++)
        {
            InputType = new PointerTypeReference(InputType);
        }

        TemporaryType = InputType;

        if (ByRefKind is ByRefKind byRefKind)
        {
            InputType     = new ByRefTypeReference(byRefKind, InputType);
            TemporaryType = new PointerTypeReference(TemporaryType);
        }
    }