From 69809fef944b5d6d64501b7bdac643a9bcfb406b Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Wed, 5 Feb 2020 12:44:53 +0300 Subject: [PATCH] [FIR] Synthetic property symbol is now a property symbol, not a function --- .../kotlin/fir/resolve/calls/Synthetics.kt | 24 +-- .../synthetic/FirSyntheticProperty.kt | 139 ++++++++++++++++++ .../synthetic/FirSyntheticPropertyAccessor.kt | 121 +++++++++++++++ .../javaSyntheticPropertyAccess.fir.txt | 6 +- .../stubs/javaSyntheticProperty.fir.txt | 2 +- .../stubs/jdkClassSyntheticProperty.fir.txt | 2 +- 6 files changed, 277 insertions(+), 17 deletions(-) create mode 100644 compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticProperty.kt create mode 100644 compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyAccessor.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt index cda94f5d1de..73dfd3e8696 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt @@ -8,15 +8,13 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction import org.jetbrains.kotlin.fir.declarations.isStatic +import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.symbols.AccessorSymbol import org.jetbrains.kotlin.fir.symbols.CallableId import org.jetbrains.kotlin.fir.symbols.StandardClassIds import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol -import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol -import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol -import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol -import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol +import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.coneTypeSafe import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName @@ -27,7 +25,7 @@ import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeFirstWord class SyntheticPropertySymbol( callableId: CallableId, override val accessorId: CallableId -) : FirNamedFunctionSymbol(callableId), AccessorSymbol, SyntheticSymbol +) : FirPropertySymbol(callableId), AccessorSymbol, SyntheticSymbol class FirSyntheticFunctionSymbol( callableId: CallableId @@ -43,7 +41,7 @@ class FirSyntheticPropertiesScope( private fun checkGetAndCreateSynthetic( name: Name, symbol: FirFunctionSymbol<*>, - processor: (FirCallableSymbol<*>) -> Unit + processor: (FirVariableSymbol<*>) -> Unit ) { val fir = symbol.fir as? FirSimpleFunction ?: return @@ -52,13 +50,15 @@ class FirSyntheticPropertiesScope( if (fir.isStatic) return if (fir.returnTypeRef.coneTypeSafe()?.lookupTag?.classId == StandardClassIds.Unit) return - val synthetic = SyntheticPropertySymbol( - accessorId = symbol.callableId, - callableId = CallableId(symbol.callableId.packageName, symbol.callableId.className, name) + val property = FirSyntheticProperty( + session, name, + symbol = SyntheticPropertySymbol( + accessorId = symbol.callableId, + callableId = CallableId(symbol.callableId.packageName, symbol.callableId.className, name) + ), + delegateGetter = fir ) - synthetic.bind(fir) - - processor(synthetic) + processor(property.symbol) } override fun processPropertiesByName(name: Name, processor: (FirCallableSymbol<*>) -> Unit) { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticProperty.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticProperty.kt new file mode 100644 index 00000000000..38d373429de --- /dev/null +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticProperty.kt @@ -0,0 +1,139 @@ +/* + * Copyright 2010-2020 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.declarations.synthetic + +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.FirSourceElement +import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl +import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.expressions.FirExpression +import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference +import org.jetbrains.kotlin.fir.references.impl.FirEmptyControlFlowGraphReference +import org.jetbrains.kotlin.fir.symbols.impl.FirBackingFieldSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirDelegateFieldSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol +import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.visitors.FirTransformer +import org.jetbrains.kotlin.fir.visitors.FirVisitor +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource + +class FirSyntheticProperty( + override val session: FirSession, + override val returnTypeRef: FirTypeRef, + override val name: Name, + override val isVar: Boolean, + override val symbol: FirPropertySymbol, + override val status: FirDeclarationStatus, + override val resolvePhase: FirResolvePhase, + override val getter: FirSyntheticPropertyAccessor, + override val setter: FirSyntheticPropertyAccessor? = null +) : FirProperty() { + init { + symbol.bind(this) + } + + constructor( + session: FirSession, + name: Name, + symbol: FirPropertySymbol, + delegateGetter: FirSimpleFunction + ) : this( + session, delegateGetter.returnTypeRef, name, false, symbol, + FirDeclarationStatusImpl(delegateGetter.visibility, delegateGetter.modality), + delegateGetter.resolvePhase, + FirSyntheticPropertyAccessor(delegateGetter, isGetter = true) + ) + + override val source: FirSourceElement? + get() = null + + override val initializer: FirExpression? + get() = null + + override val delegate: FirExpression? + get() = null + + override val delegateFieldSymbol: FirDelegateFieldSymbol? + get() = null + + override val isLocal: Boolean + get() = false + + override val receiverTypeRef: FirTypeRef? + get() = null + + override val isVal: Boolean + get() = !isVar + + override val annotations: List + get() = emptyList() + + override val typeParameters: List + get() = emptyList() + + override val containerSource: DeserializedContainerSource? + get() = null + + override val controlFlowGraphReference: FirControlFlowGraphReference = FirEmptyControlFlowGraphReference + + // ??? + override val backingFieldSymbol: FirBackingFieldSymbol = FirBackingFieldSymbol(symbol.callableId) + + override fun acceptChildren(visitor: FirVisitor, data: D) { + returnTypeRef.accept(visitor, data) + status.accept(visitor, data) + } + + override fun transformChildren(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformReturnTypeRef(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformReceiverTypeRef(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformControlFlowGraphReference(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformStatus(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformOtherChildren(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformInitializer(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformGetter(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun transformSetter(transformer: FirTransformer, data: D): FirSyntheticProperty { + throw AssertionError("Transformation of synthetic property isn't supported") + } + + override fun replaceResolvePhase(newResolvePhase: FirResolvePhase) { + throw AssertionError("Mutation of synthetic property isn't supported") + } + + override fun replaceReturnTypeRef(newReturnTypeRef: FirTypeRef) { + throw AssertionError("Mutation of synthetic property isn't supported") + } + + override fun replaceReceiverTypeRef(newReceiverTypeRef: FirTypeRef?) { + throw AssertionError("Mutation of synthetic property isn't supported") + } +} \ No newline at end of file diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyAccessor.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyAccessor.kt new file mode 100644 index 00000000000..3a25df5c80d --- /dev/null +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyAccessor.kt @@ -0,0 +1,121 @@ +/* + * Copyright 2010-2020 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.declarations.synthetic + +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.FirSourceElement +import org.jetbrains.kotlin.fir.contracts.FirContractDescription +import org.jetbrains.kotlin.fir.contracts.impl.FirEmptyContractDescription +import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.declarations.impl.FirPropertyAccessorImpl +import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.expressions.FirBlock +import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference +import org.jetbrains.kotlin.fir.references.impl.FirEmptyControlFlowGraphReference +import org.jetbrains.kotlin.fir.symbols.impl.FirPropertyAccessorSymbol +import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.visitors.FirTransformer +import org.jetbrains.kotlin.fir.visitors.FirVisitor + +class FirSyntheticPropertyAccessor( + private val delegate: FirSimpleFunction, + override val isGetter: Boolean +) : FirPropertyAccessor() { + override val source: FirSourceElement? + get() = delegate.source + + override val session: FirSession + get() = delegate.session + + override val returnTypeRef: FirTypeRef + get() = delegate.returnTypeRef + + override val resolvePhase: FirResolvePhase + get() = delegate.resolvePhase + + override val status: FirDeclarationStatus + get() = delegate.status + + override val receiverTypeRef: FirTypeRef? + get() = null + + override val valueParameters: List + get() = delegate.valueParameters + + override val annotations: List + get() = delegate.annotations + + override val typeParameters: List + get() = emptyList() + + override val isSetter: Boolean + get() = !isGetter + + override val body: FirBlock? + get() = delegate.body + + override val symbol: FirPropertyAccessorSymbol = FirPropertyAccessorSymbol().apply { + bind(this@FirSyntheticPropertyAccessor) + } + + override val controlFlowGraphReference: FirControlFlowGraphReference = FirEmptyControlFlowGraphReference + + override val contractDescription: FirContractDescription = FirEmptyContractDescription + + override fun acceptChildren(visitor: FirVisitor, data: D) { + delegate.accept(visitor, data) + controlFlowGraphReference.accept(visitor, data) + contractDescription.accept(visitor, data) + } + + override fun transformChildren(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformReturnTypeRef(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformReceiverTypeRef(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformControlFlowGraphReference(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformValueParameters(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformContractDescription(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun transformStatus(transformer: FirTransformer, data: D): FirPropertyAccessorImpl { + throw AssertionError("Transformation of synthetic property accessor isn't supported") + } + + override fun replaceResolvePhase(newResolvePhase: FirResolvePhase) { + throw AssertionError("Mutation of synthetic property accessor isn't supported") + } + + override fun replaceReturnTypeRef(newReturnTypeRef: FirTypeRef) { + throw AssertionError("Mutation of synthetic property accessor isn't supported") + } + + override fun replaceReceiverTypeRef(newReceiverTypeRef: FirTypeRef?) { + throw AssertionError("Mutation of synthetic property accessor isn't supported") + } + + override fun replaceValueParameters(newValueParameters: List) { + throw AssertionError("Mutation of synthetic property accessor isn't supported") + } + + override fun replaceContractDescription(newContractDescription: FirContractDescription) { + throw AssertionError("Mutation of synthetic property accessor isn't supported") + } +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/javaSyntheticPropertyAccess.fir.txt b/compiler/testData/ir/irText/expressions/javaSyntheticPropertyAccess.fir.txt index 598f06ed186..8b1e283b7c7 100644 --- a/compiler/testData/ir/irText/expressions/javaSyntheticPropertyAccess.fir.txt +++ b/compiler/testData/ir/irText/expressions/javaSyntheticPropertyAccess.fir.txt @@ -2,12 +2,12 @@ FILE fqName: fileName:/javaSyntheticPropertyAccess.kt FUN name:test visibility:public modality:FINAL <> (j:.J) returnType:kotlin.Unit VALUE_PARAMETER name:j index:0 type:.J BLOCK_BODY - CALL 'public open fun getFoo (): kotlin.Int [operator] declared in .J' type=kotlin.Int origin=GET_PROPERTY + CALL 'public open fun (): kotlin.Int declared in .J' type=kotlin.Int origin=null $this: GET_VAR 'j: .J declared in .test' type=.J origin=null ERROR_CALL 'Unresolved reference: R|/J.foo|' type=IrErrorType VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.Int [val] - CALL 'public open fun getFoo (): kotlin.Int [operator] declared in .J' type=kotlin.Int origin=GET_PROPERTY + CALL 'public open fun (): kotlin.Int declared in .J' type=kotlin.Int origin=null $this: GET_VAR 'j: .J declared in .test' type=.J origin=null ERROR_CALL 'Unresolved reference: R|/J.foo|' type=IrErrorType GET_VAR 'val tmp_0: kotlin.Int [val] declared in .test' type=kotlin.Int origin=null - ERROR_CALL 'Unresolved reference: #' type=IrErrorType + ERROR_CALL 'Unresolved reference: R|/J.foo|' type=IrErrorType diff --git a/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt b/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt index 791b181f035..4f9f9a97e5a 100644 --- a/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt +++ b/compiler/testData/ir/irText/stubs/javaSyntheticProperty.fir.txt @@ -2,7 +2,7 @@ FILE fqName: fileName:/javaSyntheticProperty.kt PROPERTY name:test visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:test type:kotlin.String? visibility:private [final,static] EXPRESSION_BODY - CALL 'public open fun getFoo (): kotlin.String? [operator] declared in .J' type=kotlin.String? origin=GET_PROPERTY + CALL 'public open fun (): kotlin.String? declared in .J' type=kotlin.String? origin=null $this: CONSTRUCTOR_CALL 'public/*package*/ constructor () [primary] declared in .J' type=.J origin=null FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.String? correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val] diff --git a/compiler/testData/ir/irText/stubs/jdkClassSyntheticProperty.fir.txt b/compiler/testData/ir/irText/stubs/jdkClassSyntheticProperty.fir.txt index 42e0200bb12..b8e6b7a5b82 100644 --- a/compiler/testData/ir/irText/stubs/jdkClassSyntheticProperty.fir.txt +++ b/compiler/testData/ir/irText/stubs/jdkClassSyntheticProperty.fir.txt @@ -4,5 +4,5 @@ FILE fqName: fileName:/jdkClassSyntheticProperty.kt correspondingProperty: PROPERTY name:test visibility:public modality:FINAL [val] BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun (): kotlin.Array? declared in ' - CALL 'public open fun getDeclaredFields (): kotlin.Array? [operator] declared in java.lang.Class' type=kotlin.Array? origin=GET_PROPERTY + CALL 'public open fun (): kotlin.Array? declared in java.lang.Class' type=kotlin.Array? origin=null $this: ERROR_CALL 'Unresolved reference: this@R|/test|' type=java.lang.Class<*>