FIR: implement backing field references via 'field' synthetic variable
This commit is contained in:
+47
-17
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
|
||||
import org.jetbrains.kotlin.fir.declarations.impl.FirValueParameterImpl
|
||||
import org.jetbrains.kotlin.fir.expressions.*
|
||||
import org.jetbrains.kotlin.fir.expressions.impl.FirResolvedQualifierImpl
|
||||
import org.jetbrains.kotlin.fir.references.FirBackingFieldReferenceImpl
|
||||
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
|
||||
import org.jetbrains.kotlin.fir.references.FirSimpleNamedReference
|
||||
import org.jetbrains.kotlin.fir.resolve.*
|
||||
@@ -24,6 +25,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.FirLocalScope
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.FirTopLevelDeclaredMemberScope
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.withReplacedConeType
|
||||
import org.jetbrains.kotlin.fir.symbols.*
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirBackingFieldSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.types.impl.*
|
||||
import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult
|
||||
@@ -723,24 +725,29 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
|
||||
applicability: CandidateApplicability
|
||||
): FirNamedReference {
|
||||
val name = namedReference.name
|
||||
val firSession = namedReference.session
|
||||
val psi = namedReference.psi
|
||||
return when {
|
||||
candidates.isEmpty() -> FirErrorNamedReference(
|
||||
namedReference.session, namedReference.psi, "Unresolved name: $name"
|
||||
firSession, psi, "Unresolved name: $name"
|
||||
)
|
||||
applicability < CandidateApplicability.SYNTHETIC_RESOLVED -> {
|
||||
FirErrorNamedReference(
|
||||
namedReference.session,
|
||||
namedReference.psi,
|
||||
firSession, psi,
|
||||
"Inapplicable($applicability): ${candidates.map { describeSymbol(it.symbol) }}",
|
||||
namedReference.name
|
||||
)
|
||||
}
|
||||
candidates.size == 1 -> FirNamedReferenceWithCandidate(
|
||||
namedReference.session, namedReference.psi,
|
||||
name, candidates.single()
|
||||
)
|
||||
candidates.size == 1 -> {
|
||||
val candidate = candidates.single()
|
||||
if (candidate.symbol is FirBackingFieldSymbol) {
|
||||
FirBackingFieldReferenceImpl(firSession, psi, candidate.symbol)
|
||||
} else {
|
||||
FirNamedReferenceWithCandidate(firSession, psi, name, candidate)
|
||||
}
|
||||
}
|
||||
else -> FirErrorNamedReference(
|
||||
namedReference.session, namedReference.psi, "Ambiguity: $name, ${candidates.map { describeSymbol(it.symbol) }}",
|
||||
firSession, psi, "Ambiguity: $name, ${candidates.map { describeSymbol(it.symbol) }}",
|
||||
namedReference.name
|
||||
)
|
||||
}
|
||||
@@ -845,15 +852,20 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
|
||||
replaceTypeRef(type)
|
||||
}
|
||||
|
||||
|
||||
override fun transformDeclaration(declaration: FirDeclaration, data: Any?): CompositeTransformResult<FirDeclaration> {
|
||||
private fun <T> withContainer(declaration: FirDeclaration, f: () -> T): T {
|
||||
val prevContainer = container
|
||||
container = declaration
|
||||
val result = super.transformDeclaration(declaration, data)
|
||||
val result = f()
|
||||
container = prevContainer
|
||||
return result
|
||||
}
|
||||
|
||||
override fun transformDeclaration(declaration: FirDeclaration, data: Any?): CompositeTransformResult<FirDeclaration> {
|
||||
return withContainer(declaration) {
|
||||
super.transformDeclaration(declaration, data)
|
||||
}
|
||||
}
|
||||
|
||||
override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: Any?): CompositeTransformResult<FirStatement> {
|
||||
annotationCall.resultType = annotationCall.annotationTypeRef
|
||||
return (annotationCall.transformChildren(this, data) as FirStatement).compose()
|
||||
@@ -890,8 +902,7 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
|
||||
}
|
||||
}
|
||||
|
||||
override fun transformVariable(variable: FirVariable, data: Any?): CompositeTransformResult<FirDeclaration> {
|
||||
val variable = super.transformVariable(variable, variable.returnTypeRef).single as FirVariable
|
||||
private fun storeVariableReturnType(variable: FirVariable) {
|
||||
val initializer = variable.initializer
|
||||
if (variable.returnTypeRef is FirImplicitTypeRef) {
|
||||
when {
|
||||
@@ -937,6 +948,11 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun transformVariable(variable: FirVariable, data: Any?): CompositeTransformResult<FirDeclaration> {
|
||||
val variable = super.transformVariable(variable, variable.returnTypeRef).single as FirVariable
|
||||
storeVariableReturnType(variable)
|
||||
if (variable !is FirProperty) {
|
||||
localScopes.lastOrNull()?.storeDeclaration(variable)
|
||||
}
|
||||
@@ -944,12 +960,26 @@ open class FirBodyResolveTransformer(val session: FirSession, val implicitTypeOn
|
||||
}
|
||||
|
||||
override fun transformProperty(property: FirProperty, data: Any?): CompositeTransformResult<FirDeclaration> {
|
||||
if (property.returnTypeRef !is FirImplicitTypeRef && implicitTypeOnly) return property.compose()
|
||||
val returnTypeRef = property.returnTypeRef
|
||||
if (returnTypeRef !is FirImplicitTypeRef && implicitTypeOnly) return property.compose()
|
||||
return withScopeCleanup(localScopes) {
|
||||
localScopes.addIfNotNull(primaryConstructorParametersScope)
|
||||
(transformVariable(property, data).single as FirProperty).apply {
|
||||
setter?.let { it.valueParameters[0].transformReturnTypeRef(StoreType, property.returnTypeRef) }
|
||||
}.compose()
|
||||
withContainer(property) {
|
||||
property.transformChildrenWithoutAccessors(this, returnTypeRef)
|
||||
storeVariableReturnType(property)
|
||||
withScopeCleanup(localScopes) {
|
||||
localScopes.add(FirLocalScope().apply {
|
||||
storeBackingField(property)
|
||||
})
|
||||
val enhancedTypeRef = property.returnTypeRef
|
||||
property.getter.transform<FirDeclaration, Any?>(this, enhancedTypeRef)
|
||||
property.setter?.let {
|
||||
it.transform<FirDeclaration, Any?>(this, enhancedTypeRef)
|
||||
it.valueParameters[0].transformReturnTypeRef(StoreType, enhancedTypeRef)
|
||||
}
|
||||
}
|
||||
}
|
||||
property.compose()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.scopes.impl
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirBackingFieldReference
|
||||
import org.jetbrains.kotlin.fir.declarations.FirNamedDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirNamedFunction
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.fir.expressions.FirVariable
|
||||
import org.jetbrains.kotlin.fir.scopes.FirScope
|
||||
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
|
||||
@@ -27,6 +29,10 @@ class FirLocalScope : FirScope() {
|
||||
}
|
||||
}
|
||||
|
||||
fun storeBackingField(property: FirProperty) {
|
||||
properties[FirBackingFieldReference.NAME] = property.backingFieldSymbol
|
||||
}
|
||||
|
||||
override fun processFunctionsByName(name: Name, processor: (ConeFunctionSymbol) -> ProcessorAction): ProcessorAction {
|
||||
val prop = functions[name]
|
||||
if (prop != null) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
var myProperty = listOf(1, 2, 3)
|
||||
get() {
|
||||
return field + field
|
||||
}
|
||||
set(param) {
|
||||
field = param
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
FILE: backingField.kt
|
||||
public final var myProperty: R|kotlin/collections/List<kotlin/Int>| = R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(1), Int(2), Int(3))
|
||||
public get(): R|kotlin/collections/List<kotlin/Int>| {
|
||||
^ F|/myProperty|.R|kotlin/collections/plus|<R|kotlin/Int|>(F|/myProperty|)
|
||||
}
|
||||
public set(param: R|kotlin/collections/List<kotlin/Int>|): R|kotlin/Unit| {
|
||||
F|/myProperty| = R|<local>/param|
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.util.HashSet
|
||||
|
||||
val a: MutableSet<String>? = HashSet()
|
||||
|
||||
var b: MutableSet<String>? = null
|
||||
set(_) {
|
||||
field = HashSet()
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
var c: MutableSet<String>? = null
|
||||
c = HashSet()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
FILE: hashSet.kt
|
||||
public final val a: R|kotlin/collections/MutableSet<kotlin/String>|? = R|java/util/HashSet.HashSet|<R|kotlin/Any|?>()
|
||||
public get(): R|kotlin/collections/MutableSet<kotlin/String>|?
|
||||
public final var b: R|kotlin/collections/MutableSet<kotlin/String>|? = Null(null)
|
||||
public get(): R|kotlin/collections/MutableSet<kotlin/String>|?
|
||||
public set(_: R|kotlin/collections/MutableSet<kotlin/String>|?): R|kotlin/Unit| {
|
||||
F|/b| = R|java/util/HashSet.HashSet|()
|
||||
}
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
lvar c: R|kotlin/collections/MutableSet<kotlin/String>|? = Null(null)
|
||||
R|<local>/c| = R|java/util/HashSet.HashSet|()
|
||||
}
|
||||
Generated
+10
@@ -34,6 +34,11 @@ public class FirResolveTestCaseWithStdlibGenerated extends AbstractFirResolveTes
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/arrayFirstOrNull.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("backingField.kt")
|
||||
public void testBackingField() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/backingField.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("companionLoad.kt")
|
||||
public void testCompanionLoad() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/companionLoad.kt");
|
||||
@@ -64,6 +69,11 @@ public class FirResolveTestCaseWithStdlibGenerated extends AbstractFirResolveTes
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/functionX.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("hashSet.kt")
|
||||
public void testHashSet() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/hashSet.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("helloWorld.kt")
|
||||
public void testHelloWorld() throws Exception {
|
||||
runTest("compiler/fir/resolve/testData/resolve/stdlib/helloWorld.kt");
|
||||
|
||||
Reference in New Issue
Block a user