Raw FIR: add synthesized Enum.valueOf() function
This commit is contained in:
+1
@@ -435,6 +435,7 @@ class DeclarationsConverter(
|
||||
|
||||
if (modifiers.isEnum()) {
|
||||
firClass.generateValuesFunction(session, context.packageFqName, context.className)
|
||||
firClass.generateValueOfFunction(session, context.packageFqName, context.className)
|
||||
}
|
||||
|
||||
return@withChildClassName firClass
|
||||
|
||||
@@ -12,17 +12,22 @@ import org.jetbrains.kotlin.fir.declarations.addDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirModifiableRegularClass
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirSimpleFunctionImpl
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirValueParameterImpl
|
||||
import org.jetbrains.kotlin.fir.expressions.impl.FirEmptyExpressionBlock
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl
|
||||
import org.jetbrains.kotlin.fir.types.impl.FirImplicitStringTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.impl.FirResolvedTypeRefImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
private val ENUM_VALUES = Name.identifier("values")
|
||||
private val ENUM_VALUE_OF = Name.identifier("valueOf")
|
||||
private val VALUE = Name.identifier("value")
|
||||
|
||||
fun FirModifiableRegularClass.generateValuesFunction(
|
||||
session: FirSession, packageFqName: FqName, classFqName: FqName
|
||||
@@ -48,4 +53,33 @@ fun FirModifiableRegularClass.generateValuesFunction(
|
||||
body = FirEmptyExpressionBlock()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fun FirModifiableRegularClass.generateValueOfFunction(
|
||||
session: FirSession, packageFqName: FqName, classFqName: FqName
|
||||
) {
|
||||
val symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, ENUM_VALUE_OF))
|
||||
val status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply {
|
||||
isStatic = true
|
||||
}
|
||||
addDeclaration(
|
||||
FirSimpleFunctionImpl(
|
||||
source, session,
|
||||
FirResolvedTypeRefImpl(
|
||||
source, ConeClassTypeImpl(
|
||||
ConeClassLikeLookupTagImpl(this.symbol.classId),
|
||||
emptyArray(),
|
||||
isNullable = false
|
||||
)
|
||||
),
|
||||
null, ENUM_VALUE_OF, status, symbol
|
||||
).apply {
|
||||
valueParameters += FirValueParameterImpl(
|
||||
source, session, FirImplicitStringTypeRef(source),
|
||||
VALUE, FirVariableSymbol(VALUE),
|
||||
defaultValue = null, isCrossinline = false, isNoinline = false, isVararg = false
|
||||
)
|
||||
body = FirEmptyExpressionBlock()
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -537,6 +537,7 @@ class RawFirBuilder(session: FirSession, val stubMode: Boolean) : BaseFirBuilder
|
||||
|
||||
if (classOrObject.hasModifier(ENUM_KEYWORD)) {
|
||||
firClass.generateValuesFunction(session, context.packageFqName, context.className)
|
||||
firClass.generateValueOfFunction(session, context.packageFqName, context.className)
|
||||
}
|
||||
|
||||
firClass
|
||||
|
||||
@@ -28,6 +28,9 @@ FILE: enums.kt
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|Order| {
|
||||
}
|
||||
|
||||
}
|
||||
public? final? enum class Planet : R|kotlin/Enum| {
|
||||
public? constructor(m: Double, r: Double): R|Planet| {
|
||||
@@ -91,4 +94,7 @@ FILE: enums.kt
|
||||
public final static fun values(): R|kotlin/Array<Planet>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|Planet| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,4 +48,7 @@ FILE: enums2.kt
|
||||
public final static fun values(): R|kotlin/Array<SomeEnum>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|SomeEnum| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,4 +48,7 @@ FILE: enum.kt
|
||||
public final static fun values(): R|kotlin/Array<SomeEnum>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|SomeEnum| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,9 @@ FILE: exhaustiveness_enum.kt
|
||||
public final static fun values(): R|kotlin/Array<Enum>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|Enum| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun test_1(e: R|Enum|): R|kotlin/Unit| {
|
||||
lval a: R|kotlin/Unit| = when (R|<local>/e|) {
|
||||
|
||||
@@ -12,4 +12,7 @@ fun foo() {
|
||||
for (value in values) {
|
||||
value.bar()
|
||||
}
|
||||
|
||||
val first = MyEnum.valueOf("FIRST")
|
||||
val last = MyEnum.valueOf("LAST")
|
||||
}
|
||||
@@ -32,6 +32,9 @@ FILE: enumValues.kt
|
||||
public final static fun values(): R|kotlin/Array<MyEnum>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|MyEnum| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
lval values: R|kotlin/Array<MyEnum>| = Q|MyEnum|.R|/MyEnum.values|()
|
||||
@@ -42,4 +45,6 @@ FILE: enumValues.kt
|
||||
R|<local>/value|.R|/MyEnum.bar|()
|
||||
}
|
||||
|
||||
lval first: R|MyEnum| = Q|MyEnum|.R|/MyEnum.valueOf|(String(FIRST))
|
||||
lval last: R|MyEnum| = Q|MyEnum|.R|/MyEnum.valueOf|(String(LAST))
|
||||
}
|
||||
|
||||
@@ -43,6 +43,9 @@ FILE: qualifiedExpressions.kt
|
||||
public final static fun values(): R|kotlin/Array<a/b/E>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|a/b/E| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
@@ -28,6 +28,9 @@ FILE: enums.kt
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|Order| {
|
||||
}
|
||||
|
||||
}
|
||||
public final enum class Planet : R|kotlin/Enum| {
|
||||
private constructor(m: R|kotlin/Double|, r: R|kotlin/Double|): R|Planet| {
|
||||
@@ -91,4 +94,7 @@ FILE: enums.kt
|
||||
public final static fun values(): R|kotlin/Array<Planet>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|Planet| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user