Support unsigned types and array specializations in stdlib generator

This commit is contained in:
Ilya Gorbunov
2018-08-15 17:40:12 +03:00
parent 76ff4c9b2f
commit add0894f54
4 changed files with 26 additions and 8 deletions
@@ -14,6 +14,7 @@ enum class Family {
InvariantArraysOfObjects,
ArraysOfObjects,
ArraysOfPrimitives,
ArraysOfUnsigned,
Sequences,
CharSequences,
Strings,
@@ -21,7 +22,8 @@ enum class Family {
RangesOfPrimitives,
ProgressionsOfPrimitives,
Generic,
Primitives;
Primitives,
Unsigned;
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
@@ -44,18 +46,27 @@ enum class PrimitiveType {
Float,
Double,
Boolean,
Char;
Char,
// unsigned
UByte,
UShort,
UInt,
ULong;
val capacity by lazy { descendingByDomainCapacity.indexOf(this).let { if (it < 0) it else descendingByDomainCapacity.size - it } }
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 integralPrimitives = setOf(Int, Long, Byte, Short, Char)
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
Ranges, RangesOfPrimitives, ProgressionsOfPrimitives -> SourceFile.Ranges
ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives -> SourceFile.Arrays
ArraysOfUnsigned -> SourceFile.UArrays
Maps -> SourceFile.Maps
Strings -> SourceFile.Strings
CharSequences -> SourceFile.Strings
Primitives, Generic -> SourceFile.Misc
Primitives, Generic, Unsigned -> SourceFile.Misc
}
@TemplateDsl
@@ -258,10 +259,10 @@ class MemberBuilder(
Strings -> "String"
CharSequences -> "CharSequence"
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")
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
})
@@ -8,11 +8,13 @@ package templates
enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = true, val packageName: String? = null) {
Arrays(packageName = "kotlin.collections"),
UArrays(packageName = "kotlin.collections"),
Collections(packageName = "kotlin.collections"),
Sets(packageName = "kotlin.collections"),
Maps(packageName = "kotlin.collections"),
Sequences(packageName = "kotlin.sequences"),
Ranges(packageName = "kotlin.ranges"),
URanges(packageName = "kotlin.ranges"),
Comparisons(packageName = "kotlin.comparisons"),
Strings(packageName = "kotlin.text"),
Misc(),
@@ -147,7 +147,11 @@ abstract class MemberTemplateDefinition<TParam> : MemberTemplate {
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
class FamilyPrimitiveMemberDefinition : MemberTemplateDefinition<PrimitiveType?>() {