Biohazrd icon indicating copy to clipboard operation
Biohazrd copied to clipboard

Fix support for constant arrays of void*

Open PathogenDavid opened this issue 2 years ago • 1 comments

Right now constant arrays of void* tries to emit an array helper with the following enumerator:

public ConstantArrayOfPointersEnumerator<void> GetEnumerator()
    => new ConstantArrayOfPointersEnumerator<void>(Element0Pointer, 2000);

This isn't valid since void cannot be a generic type argument. We need to special-case void* arrays.

PathogenDavid avatar Oct 21 '21 14:10 PathogenDavid

Workaround transformation to rewrite the constant arrays to byte*[] arrays:

using Biohazrd;
using Biohazrd.CSharp;
using Biohazrd.Transformation;

namespace Mochi.MuJoCo.Generator
{
    // This is a workaround for https://github.com/InfectedLibraries/Biohazrd/issues/221
    internal sealed class __WorkaroundBiohazrd221Transformation : CSharpTransformationBase
    {
        protected override TransformationResult TransformConstantArrayType(TransformationContext context, ConstantArrayTypeDeclaration declaration)
        {
            if (declaration.Type == VoidTypeReference.PointerInstance)
            {
                return declaration with { Type = new PointerTypeReference(CSharpBuiltinType.Byte) };
            }
            else
            { return declaration; }
        }
    }
}

(Note: This doesn't properly handle void** and other levels of indirection.)

PathogenDavid avatar Oct 21 '21 14:10 PathogenDavid