diff --git a/core/src/fr/hammons/slinc/DescriptorOf.scala b/core/src/fr/hammons/slinc/DescriptorOf.scala index 33d1bbf..f819886 100644 --- a/core/src/fr/hammons/slinc/DescriptorOf.scala +++ b/core/src/fr/hammons/slinc/DescriptorOf.scala @@ -2,7 +2,7 @@ package fr.hammons.slinc import fr.hammons.slinc.container.* import scala.quoted.* -import scala.compiletime.{summonInline, erasedValue} +import scala.compiletime.{summonInline, erasedValue, constValue} import scala.NonEmptyTuple /** Typeclass that summons TypeDescriptors @@ -77,3 +77,11 @@ object DescriptorOf: val descriptor: CUnionDescriptor { type Inner = CUnion[A] } = CUnionDescriptor(helper[A]) .asInstanceOf[CUnionDescriptor { type Inner = CUnion[A] }] + + inline given [A, B <: Int](using + innerDesc: DescriptorOf[A] + ): DescriptorOf[SetSizeArray[A, B]] = new DescriptorOf[SetSizeArray[A, B]]: + val descriptor: TypeDescriptor { type Inner = SetSizeArray[A, B] } = + SetSizeArrayDescriptor(innerDesc.descriptor, constValue[B]).asInstanceOf[ + SetSizeArrayDescriptor { type Inner = SetSizeArray[A, B] } + ] diff --git a/core/src/fr/hammons/slinc/TypeDescriptor.scala b/core/src/fr/hammons/slinc/TypeDescriptor.scala index ce1d46d..fc97d2b 100644 --- a/core/src/fr/hammons/slinc/TypeDescriptor.scala +++ b/core/src/fr/hammons/slinc/TypeDescriptor.scala @@ -14,6 +14,7 @@ import fr.hammons.slinc.modules.TransitionModule import fr.hammons.slinc.modules.{ArgumentTransition, ReturnTransition} import scala.NonEmptyTuple import scala.language.implicitConversions +import dotty.tools.dotc.transform.patmat.Typ /** Describes types used by C interop */ @@ -215,3 +216,24 @@ case class CUnionDescriptor(possibleTypes: Set[TypeDescriptor]) override val writer: (ReadWriteModule, DescriptorModule) ?=> Writer[Inner] = summon[ReadWriteModule].unionWriter(this) + +case class SetSizeArrayDescriptor( + val contained: TypeDescriptor, + val number: Int +) extends TypeDescriptor: + + override val reader: (ReadWriteModule, DescriptorModule) ?=> Reader[Inner] = + ??? + + override val writer: (ReadWriteModule, DescriptorModule) ?=> Writer[Inner] = + ??? + + override val argumentTransition + : (TransitionModule, ReadWriteModule, Allocator) ?=> ArgumentTransition[ + Inner + ] = ??? + + override val returnTransition + : (TransitionModule, ReadWriteModule) ?=> ReturnTransition[Inner] = ??? + + type Inner = SetSizeArray[?, ?] diff --git a/core/test/src/fr/hammons/slinc/modules/DescriptorSpec.scala b/core/test/src/fr/hammons/slinc/modules/DescriptorSpec.scala index 1ad6703..2519844 100644 --- a/core/test/src/fr/hammons/slinc/modules/DescriptorSpec.scala +++ b/core/test/src/fr/hammons/slinc/modules/DescriptorSpec.scala @@ -14,6 +14,7 @@ import fr.hammons.slinc.IntDescriptor import fr.hammons.slinc.LongDescriptor import fr.hammons.slinc.FloatDescriptor import fr.hammons.slinc.DoubleDescriptor +import fr.hammons.slinc.SetSizeArray trait DescriptorSpec(val slinc: Slinc) extends munit.FunSuite: import slinc.dm @@ -60,3 +61,15 @@ trait DescriptorSpec(val slinc: Slinc) extends munit.FunSuite: CLongLong ].size * 2) ) + + test("SetSizeArrayDescriptor is size of inner type * num"): + assertEquals( + DescriptorOf[SetSizeArray[CInt, 15]].size, + DescriptorOf[CInt].size * 15 + ) + + test("SetSizeArrayDescriptor is alignment of inner type"): + assertEquals( + DescriptorOf[SetSizeArray[CInt, 15]].alignment, + DescriptorOf[CInt].alignment + ) diff --git a/j17/src/fr/hammons/slinc/modules/DescriptorModule17.scala b/j17/src/fr/hammons/slinc/modules/DescriptorModule17.scala index 440fdf3..ac298b5 100644 --- a/j17/src/fr/hammons/slinc/modules/DescriptorModule17.scala +++ b/j17/src/fr/hammons/slinc/modules/DescriptorModule17.scala @@ -19,17 +19,18 @@ given descriptorModule17: DescriptorModule with val offsets: TrieMap[List[TypeDescriptor], IArray[Bytes]] = TrieMap.empty def toCarrierType(td: TypeDescriptor): Class[?] = td match - case ByteDescriptor => classOf[Byte] - case ShortDescriptor => classOf[Short] - case IntDescriptor => classOf[Int] - case LongDescriptor => classOf[Long] - case FloatDescriptor => classOf[Float] - case DoubleDescriptor => classOf[Double] - case PtrDescriptor => classOf[MemoryAddress] - case _: StructDescriptor => classOf[MemorySegment] + case ByteDescriptor => classOf[Byte] + case ShortDescriptor => classOf[Short] + case IntDescriptor => classOf[Int] + case LongDescriptor => classOf[Long] + case FloatDescriptor => classOf[Float] + case DoubleDescriptor => classOf[Double] + case PtrDescriptor => classOf[MemoryAddress] + case _: StructDescriptor | _: CUnionDescriptor | + _: SetSizeArrayDescriptor => + classOf[MemorySegment] case VaListDescriptor => classOf[MemoryAddress] case ad: AliasDescriptor[?] => toCarrierType(ad.real) - case ud: CUnionDescriptor => classOf[MemorySegment] def genLayoutList( layouts: Seq[MemoryLayout], @@ -117,6 +118,8 @@ given descriptorModule17: DescriptorModule with case VaListDescriptor => C_POINTER.nn case sd: StructDescriptor => toGroupLayout(sd) case ad: AliasDescriptor[?] => toMemoryLayout(ad.real) + case SetSizeArrayDescriptor(inner, num) => + MemoryLayout.sequenceLayout(num, toMemoryLayout(inner)).nn case CUnionDescriptor(possibleTypes) => MemoryLayout.unionLayout(possibleTypes.map(toMemoryLayout).toSeq*).nn diff --git a/j19/src/fr/hammons/slinc/modules/DescriptorModule19.scala b/j19/src/fr/hammons/slinc/modules/DescriptorModule19.scala index 56efe37..614ccd8 100644 --- a/j19/src/fr/hammons/slinc/modules/DescriptorModule19.scala +++ b/j19/src/fr/hammons/slinc/modules/DescriptorModule19.scala @@ -13,14 +13,16 @@ given descriptorModule19: DescriptorModule with private val offsets = TrieMap.empty[List[TypeDescriptor], IArray[Bytes]] def toMemoryLayout(td: TypeDescriptor): MemoryLayout = td match - case ByteDescriptor => ValueLayout.JAVA_BYTE.nn - case ShortDescriptor => ValueLayout.JAVA_SHORT.nn - case IntDescriptor => ValueLayout.JAVA_INT.nn - case LongDescriptor => ValueLayout.JAVA_LONG.nn - case FloatDescriptor => ValueLayout.JAVA_FLOAT.nn - case DoubleDescriptor => ValueLayout.JAVA_DOUBLE.nn - case PtrDescriptor => ValueLayout.ADDRESS.nn - case VaListDescriptor => ValueLayout.ADDRESS.nn + case ByteDescriptor => ValueLayout.JAVA_BYTE.nn + case ShortDescriptor => ValueLayout.JAVA_SHORT.nn + case IntDescriptor => ValueLayout.JAVA_INT.nn + case LongDescriptor => ValueLayout.JAVA_LONG.nn + case FloatDescriptor => ValueLayout.JAVA_FLOAT.nn + case DoubleDescriptor => ValueLayout.JAVA_DOUBLE.nn + case PtrDescriptor => ValueLayout.ADDRESS.nn + case VaListDescriptor => ValueLayout.ADDRESS.nn + case SetSizeArrayDescriptor(inner, num) => + MemoryLayout.sequenceLayout(num, toMemoryLayout(inner)).nn case sd: StructDescriptor => toGroupLayout(sd) case ad: AliasDescriptor[?] => toMemoryLayout(ad.real) case CUnionDescriptor(possibleTypes) => @@ -78,17 +80,18 @@ given descriptorModule19: DescriptorModule with ) def toCarrierType(td: TypeDescriptor): Class[?] = td match - case ByteDescriptor => classOf[Byte] - case ShortDescriptor => classOf[Short] - case IntDescriptor => classOf[Int] - case LongDescriptor => classOf[Long] - case FloatDescriptor => classOf[Float] - case DoubleDescriptor => classOf[Double] - case VaListDescriptor => classOf[MemoryAddress] - case PtrDescriptor => classOf[MemoryAddress] - case _: StructDescriptor => classOf[MemorySegment] + case ByteDescriptor => classOf[Byte] + case ShortDescriptor => classOf[Short] + case IntDescriptor => classOf[Int] + case LongDescriptor => classOf[Long] + case FloatDescriptor => classOf[Float] + case DoubleDescriptor => classOf[Double] + case VaListDescriptor => classOf[MemoryAddress] + case PtrDescriptor => classOf[MemoryAddress] + case _: StructDescriptor | _: CUnionDescriptor | + _: SetSizeArrayDescriptor => + classOf[MemorySegment] case ad: AliasDescriptor[?] => toCarrierType(ad.real) - case CUnionDescriptor(_) => classOf[MemorySegment] override def memberOffsets(sd: List[TypeDescriptor]): IArray[Bytes] = offsets.getOrElseUpdate(