Raw FIR: add synthesized Enum.values() function #KT-24076 Fixed
This commit is contained in:
+5
-4
@@ -16,10 +16,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.NAME_FOR_DEFAULT_VALUE_PARAMETER
|
||||
import org.jetbrains.kotlin.fir.builder.Context
|
||||
import org.jetbrains.kotlin.fir.builder.generateAccessorsByDelegate
|
||||
import org.jetbrains.kotlin.fir.builder.generateComponentFunctions
|
||||
import org.jetbrains.kotlin.fir.builder.generateCopyFunction
|
||||
import org.jetbrains.kotlin.fir.builder.*
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.*
|
||||
import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
|
||||
@@ -436,6 +433,10 @@ class DeclarationsConverter(
|
||||
// TODO: equals, hashCode, toString
|
||||
}
|
||||
|
||||
if (modifiers.isEnum()) {
|
||||
firClass.generateValuesFunction(session, context.packageFqName, context.className)
|
||||
}
|
||||
|
||||
return@withChildClassName firClass
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.builder
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
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.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.types.impl.ConeClassTypeImpl
|
||||
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")
|
||||
|
||||
fun FirModifiableRegularClass.generateValuesFunction(
|
||||
session: FirSession, packageFqName: FqName, classFqName: FqName
|
||||
) {
|
||||
val symbol = FirNamedFunctionSymbol(CallableId(packageFqName, classFqName, ENUM_VALUES))
|
||||
val status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply {
|
||||
isStatic = true
|
||||
}
|
||||
addDeclaration(
|
||||
FirSimpleFunctionImpl(
|
||||
source, session,
|
||||
FirResolvedTypeRefImpl(
|
||||
source, ConeClassTypeImpl(
|
||||
ConeClassLikeLookupTagImpl(StandardClassIds.Array),
|
||||
arrayOf(
|
||||
ConeClassTypeImpl(ConeClassLikeLookupTagImpl(this.symbol.classId), emptyArray(), isNullable = false)
|
||||
),
|
||||
isNullable = false
|
||||
)
|
||||
),
|
||||
null, ENUM_VALUES, status, symbol
|
||||
).apply {
|
||||
body = FirEmptyExpressionBlock()
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -535,6 +535,10 @@ class RawFirBuilder(session: FirSession, val stubMode: Boolean) : BaseFirBuilder
|
||||
// TODO: equals, hashCode, toString
|
||||
}
|
||||
|
||||
if (classOrObject.hasModifier(ENUM_KEYWORD)) {
|
||||
firClass.generateValuesFunction(session, context.packageFqName, context.className)
|
||||
}
|
||||
|
||||
firClass
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ FILE: enums.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
}
|
||||
public? final? enum class Planet : R|kotlin/Enum| {
|
||||
public? constructor(m: Double, r: Double): R|Planet| {
|
||||
@@ -85,4 +88,7 @@ FILE: enums.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Planet>| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,4 +45,7 @@ FILE: enums2.kt
|
||||
|
||||
public? abstract fun check(y: Some): Boolean
|
||||
|
||||
public final static fun values(): R|kotlin/Array<SomeEnum>| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,4 +45,7 @@ FILE: enum.kt
|
||||
|
||||
public abstract fun check(y: R|Some|): R|kotlin/Boolean|
|
||||
|
||||
public final static fun values(): R|kotlin/Array<SomeEnum>| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ FILE: exhaustiveness_enum.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Enum>| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun test_1(e: R|Enum|): R|kotlin/Unit| {
|
||||
lval a: R|kotlin/Unit| = when (R|<local>/e|) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
enum class MyEnum {
|
||||
FIRST,
|
||||
SECOND,
|
||||
LAST;
|
||||
|
||||
fun bar() = 42
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
val values = MyEnum.values()
|
||||
|
||||
for (value in values) {
|
||||
value.bar()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
FILE: enumValues.kt
|
||||
public final enum class MyEnum : R|kotlin/Enum| {
|
||||
private constructor(): R|MyEnum| {
|
||||
super<R|kotlin/Enum|>()
|
||||
}
|
||||
|
||||
public final enum entry FIRST : R|kotlin/Any| {
|
||||
public constructor(): R|MyEnum.FIRST| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final enum entry SECOND : R|kotlin/Any| {
|
||||
public constructor(): R|MyEnum.SECOND| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final enum entry LAST : R|kotlin/Any| {
|
||||
public constructor(): R|MyEnum.LAST| {
|
||||
super<R|kotlin/Any|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final fun bar(): R|kotlin/Int| {
|
||||
^bar Int(42)
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<MyEnum>| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
lval values: R|kotlin/Array<MyEnum>| = Q|MyEnum|.R|/MyEnum.values|()
|
||||
lval <range>: R|kotlin/Array<MyEnum>| = R|<local>/values|
|
||||
lval <iterator>: R|kotlin/collections/Iterator<MyEnum>| = R|<local>/<range>|.R|FakeOverride<kotlin/Array.iterator: R|kotlin/collections/Iterator<MyEnum>|>|()
|
||||
while(R|<local>/<iterator>|.R|kotlin/collections/Iterator.hasNext|()) {
|
||||
lval value: R|MyEnum| = R|<local>/<iterator>|.R|FakeOverride<kotlin/collections/Iterator.next: R|MyEnum|>|()
|
||||
R|<local>/value|.R|/MyEnum.bar|()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,6 +40,9 @@ FILE: qualifiedExpressions.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<a/b/E>| {
|
||||
}
|
||||
|
||||
}
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@ FILE: enums.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
}
|
||||
public final enum class Planet : R|kotlin/Enum| {
|
||||
private constructor(m: R|kotlin/Double|, r: R|kotlin/Double|): R|Planet| {
|
||||
@@ -85,4 +88,7 @@ FILE: enums.kt
|
||||
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<Planet>| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
@@ -334,6 +334,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
|
||||
runTest("compiler/fir/resolve/testData/resolve/expresssions/dispatchReceiver.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("enumValues.kt")
|
||||
public void testEnumValues() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/expresssions/enumValues.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extensionPropertyInLambda.kt")
|
||||
public void testExtensionPropertyInLambda() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/expresssions/extensionPropertyInLambda.kt");
|
||||
|
||||
Reference in New Issue
Block a user