Support unsigned types and array specializations in stdlib generator
This commit is contained in:
@@ -14,6 +14,7 @@ enum class Family {
|
|||||||
InvariantArraysOfObjects,
|
InvariantArraysOfObjects,
|
||||||
ArraysOfObjects,
|
ArraysOfObjects,
|
||||||
ArraysOfPrimitives,
|
ArraysOfPrimitives,
|
||||||
|
ArraysOfUnsigned,
|
||||||
Sequences,
|
Sequences,
|
||||||
CharSequences,
|
CharSequences,
|
||||||
Strings,
|
Strings,
|
||||||
@@ -21,7 +22,8 @@ enum class Family {
|
|||||||
RangesOfPrimitives,
|
RangesOfPrimitives,
|
||||||
ProgressionsOfPrimitives,
|
ProgressionsOfPrimitives,
|
||||||
Generic,
|
Generic,
|
||||||
Primitives;
|
Primitives,
|
||||||
|
Unsigned;
|
||||||
|
|
||||||
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
|
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
|
||||||
|
|
||||||
@@ -44,18 +46,27 @@ enum class PrimitiveType {
|
|||||||
Float,
|
Float,
|
||||||
Double,
|
Double,
|
||||||
Boolean,
|
Boolean,
|
||||||
Char;
|
Char,
|
||||||
|
// unsigned
|
||||||
|
UByte,
|
||||||
|
UShort,
|
||||||
|
UInt,
|
||||||
|
ULong;
|
||||||
|
|
||||||
val capacity by lazy { descendingByDomainCapacity.indexOf(this).let { if (it < 0) it else descendingByDomainCapacity.size - it } }
|
val capacity by lazy { descendingByDomainCapacity.indexOf(this).let { if (it < 0) it else descendingByDomainCapacity.size - it } }
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val defaultPrimitives = PrimitiveType.values().toSet()
|
val unsignedPrimitives = setOf(UInt, ULong, UByte, UShort)
|
||||||
|
val defaultPrimitives = PrimitiveType.values().toSet() - unsignedPrimitives
|
||||||
val numericPrimitives = setOf(Int, Long, Byte, Short, Double, Float)
|
val numericPrimitives = setOf(Int, Long, Byte, Short, Double, Float)
|
||||||
val integralPrimitives = setOf(Int, Long, Byte, Short, Char)
|
val integralPrimitives = setOf(Int, Long, Byte, Short, Char)
|
||||||
|
|
||||||
val descendingByDomainCapacity = listOf(Double, Float, Long, Int, Short, Char, Byte)
|
val descendingByDomainCapacity = listOf(Double, Float, Long, Int, Short, Char, Byte)
|
||||||
|
val descendingByDomainCapacityUnsigned = listOf(ULong, UInt, UShort, UByte)
|
||||||
|
|
||||||
fun maxByCapacity(fromType: PrimitiveType, toType: PrimitiveType): PrimitiveType = descendingByDomainCapacity.first { it == fromType || it == toType }
|
fun maxByCapacity(fromType: PrimitiveType, toType: PrimitiveType): PrimitiveType =
|
||||||
|
(if (fromType in unsignedPrimitives) descendingByDomainCapacityUnsigned else descendingByDomainCapacity)
|
||||||
|
.first { it == fromType || it == toType }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,11 @@ private fun getDefaultSourceFile(f: Family): SourceFile = when (f) {
|
|||||||
Sets -> SourceFile.Sets
|
Sets -> SourceFile.Sets
|
||||||
Ranges, RangesOfPrimitives, ProgressionsOfPrimitives -> SourceFile.Ranges
|
Ranges, RangesOfPrimitives, ProgressionsOfPrimitives -> SourceFile.Ranges
|
||||||
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
|
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
|
||||||
|
ArraysOfUnsigned -> SourceFile.UArrays
|
||||||
Maps -> SourceFile.Maps
|
Maps -> SourceFile.Maps
|
||||||
Strings -> SourceFile.Strings
|
Strings -> SourceFile.Strings
|
||||||
CharSequences -> SourceFile.Strings
|
CharSequences -> SourceFile.Strings
|
||||||
Primitives, Generic -> SourceFile.Misc
|
Primitives, Generic, Unsigned -> SourceFile.Misc
|
||||||
}
|
}
|
||||||
|
|
||||||
@TemplateDsl
|
@TemplateDsl
|
||||||
@@ -258,10 +259,10 @@ class MemberBuilder(
|
|||||||
Strings -> "String"
|
Strings -> "String"
|
||||||
CharSequences -> "CharSequence"
|
CharSequences -> "CharSequence"
|
||||||
Ranges -> "ClosedRange<$isAsteriskOrT>"
|
Ranges -> "ClosedRange<$isAsteriskOrT>"
|
||||||
ArraysOfPrimitives -> primitive?.let { it.name + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type")
|
ArraysOfPrimitives, ArraysOfUnsigned -> primitive?.let { it.name + "Array" } ?: throw IllegalArgumentException("Primitive array should specify primitive type")
|
||||||
RangesOfPrimitives -> primitive?.let { it.name + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type")
|
RangesOfPrimitives -> primitive?.let { it.name + "Range" } ?: throw IllegalArgumentException("Primitive range should specify primitive type")
|
||||||
ProgressionsOfPrimitives -> primitive?.let { it.name + "Progression" } ?: throw IllegalArgumentException("Primitive progression should specify primitive type")
|
ProgressionsOfPrimitives -> primitive?.let { it.name + "Progression" } ?: throw IllegalArgumentException("Primitive progression should specify primitive type")
|
||||||
Primitives -> primitive?.let { it.name } ?: throw IllegalArgumentException("Primitive should specify primitive type")
|
Primitives, Unsigned -> primitive?.let { it.name } ?: throw IllegalArgumentException("Primitive should specify primitive type")
|
||||||
Generic -> primaryTypeParameter
|
Generic -> primaryTypeParameter
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ package templates
|
|||||||
enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = true, val packageName: String? = null) {
|
enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = true, val packageName: String? = null) {
|
||||||
|
|
||||||
Arrays(packageName = "kotlin.collections"),
|
Arrays(packageName = "kotlin.collections"),
|
||||||
|
UArrays(packageName = "kotlin.collections"),
|
||||||
Collections(packageName = "kotlin.collections"),
|
Collections(packageName = "kotlin.collections"),
|
||||||
Sets(packageName = "kotlin.collections"),
|
Sets(packageName = "kotlin.collections"),
|
||||||
Maps(packageName = "kotlin.collections"),
|
Maps(packageName = "kotlin.collections"),
|
||||||
Sequences(packageName = "kotlin.sequences"),
|
Sequences(packageName = "kotlin.sequences"),
|
||||||
Ranges(packageName = "kotlin.ranges"),
|
Ranges(packageName = "kotlin.ranges"),
|
||||||
|
URanges(packageName = "kotlin.ranges"),
|
||||||
Comparisons(packageName = "kotlin.comparisons"),
|
Comparisons(packageName = "kotlin.comparisons"),
|
||||||
Strings(packageName = "kotlin.text"),
|
Strings(packageName = "kotlin.text"),
|
||||||
Misc(),
|
Misc(),
|
||||||
|
|||||||
@@ -147,7 +147,11 @@ abstract class MemberTemplateDefinition<TParam> : MemberTemplate {
|
|||||||
|
|
||||||
|
|
||||||
private fun defaultPrimitives(f: Family): Set<PrimitiveType> =
|
private fun defaultPrimitives(f: Family): Set<PrimitiveType> =
|
||||||
if (f.isPrimitiveSpecialization) PrimitiveType.defaultPrimitives else emptySet()
|
when {
|
||||||
|
f == Family.Unsigned || f == Family.ArraysOfUnsigned -> PrimitiveType.unsignedPrimitives
|
||||||
|
f.isPrimitiveSpecialization -> PrimitiveType.defaultPrimitives
|
||||||
|
else -> emptySet()
|
||||||
|
}
|
||||||
|
|
||||||
@TemplateDsl
|
@TemplateDsl
|
||||||
class FamilyPrimitiveMemberDefinition : MemberTemplateDefinition<PrimitiveType?>() {
|
class FamilyPrimitiveMemberDefinition : MemberTemplateDefinition<PrimitiveType?>() {
|
||||||
|
|||||||
Reference in New Issue
Block a user