From d19d52292e086530c69cf3fe8e0a0eeefa1d57ce Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 10 Feb 2020 12:32:27 +0300 Subject: [PATCH] [FIR] Use builder to create synthetic properties --- .../java/scopes/JavaClassEnhancementScope.kt | 12 ++++--- .../scopes/JavaClassUseSiteMemberScope.kt | 11 ++++--- .../kotlin/fir/resolve/calls/Synthetics.kt | 12 ++++--- .../scopes/impl/FirClassSubstitutionScope.kt | 11 ++++--- .../synthetic/FirSyntheticProperty.kt | 16 +-------- .../synthetic/FirSyntheticPropertyBuilder.kt | 33 +++++++++++++++++++ 6 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyBuilder.kt diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt index 6842510069b..ac97716f397 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.declarations.builder.FirSimpleFunctionBuilder import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter import org.jetbrains.kotlin.fir.declarations.impl.* import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty +import org.jetbrains.kotlin.fir.declarations.synthetic.buildSyntheticProperty import org.jetbrains.kotlin.fir.expressions.FirConstKind import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression @@ -110,11 +111,12 @@ class JavaClassEnhancementScope( val enhancedFunctionSymbol = enhanceMethod( firElement.getter.delegate, accessorSymbol.accessorId, accessorSymbol.accessorId.callableName ) - val enhancedProperty = FirSyntheticProperty( - session, name, FirAccessorSymbol(accessorSymbol.callableId, accessorSymbol.accessorId), - enhancedFunctionSymbol.fir as FirSimpleFunction - ) - return enhancedProperty.symbol + return buildSyntheticProperty { + session = this@JavaClassEnhancementScope.session + this.name = name + symbol = FirAccessorSymbol(accessorSymbol.callableId, accessorSymbol.accessorId) + delegateGetter = enhancedFunctionSymbol.fir as FirSimpleFunction + }.symbol } else -> { if (original is FirPropertySymbol || original is FirAccessorSymbol) return original diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt index 72d9b94b66a..96e88f6b6b4 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.builder.FirSimpleFunctionBuilder import org.jetbrains.kotlin.fir.declarations.builder.FirValueParameterBuilder import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty +import org.jetbrains.kotlin.fir.declarations.synthetic.buildSyntheticProperty import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack import org.jetbrains.kotlin.fir.java.declarations.* @@ -66,15 +67,15 @@ class JavaClassUseSiteMemberScope( } } overrideCandidates += functionSymbol - val accessorProperty = FirSyntheticProperty( - session, syntheticPropertyName, + return buildSyntheticProperty { + session = this@JavaClassUseSiteMemberScope.session + name = syntheticPropertyName symbol = FirAccessorSymbol( accessorId = functionSymbol.callableId, callableId = CallableId(functionSymbol.callableId.packageName, functionSymbol.callableId.className, syntheticPropertyName) - ), + ) delegateGetter = fir - ) - return accessorProperty.symbol + }.symbol } private fun processAccessorFunctionsAndPropertiesByName( 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 ad60c1f11c4..a4078e2f351 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 @@ -9,6 +9,7 @@ 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.declarations.synthetic.buildSyntheticProperty import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.symbols.CallableId import org.jetbrains.kotlin.fir.symbols.StandardClassIds @@ -65,15 +66,16 @@ class FirSyntheticPropertiesScope( }) } - val property = FirSyntheticProperty( - session, propertyName, + val property = buildSyntheticProperty { + session = this@FirSyntheticPropertiesScope.session + name = propertyName symbol = SyntheticPropertySymbol( accessorId = getterSymbol.callableId, callableId = CallableId(getterSymbol.callableId.packageName, getterSymbol.callableId.className, propertyName) - ), - delegateGetter = getter, + ) + delegateGetter = getter delegateSetter = matchingSetter - ) + } processor(property.symbol) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt index 2a7d0ac7cdb..152f0060382 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction import org.jetbrains.kotlin.fir.declarations.FirTypeParameter import org.jetbrains.kotlin.fir.declarations.builder.* import org.jetbrains.kotlin.fir.declarations.synthetic.FirSyntheticProperty +import org.jetbrains.kotlin.fir.declarations.synthetic.buildSyntheticProperty import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor @@ -333,10 +334,12 @@ class FirClassSubstitutionScope( val function = createFakeOverrideFunction( functionSymbol, session, baseProperty.getter.delegate, null, newReturnType, newParameterTypes ) - val property = FirSyntheticProperty( - session, baseProperty.name, FirAccessorSymbol(baseSymbol.callableId, baseSymbol.accessorId), function - ) - return property.symbol + return buildSyntheticProperty { + this.session = session + name = baseProperty.name + symbol = FirAccessorSymbol(baseSymbol.callableId, baseSymbol.accessorId) + delegateGetter = function + }.symbol } } } 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 index cf74b7cb12b..ce4dd43f1e6 100644 --- 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 @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitor import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource -class FirSyntheticProperty private constructor( +class FirSyntheticProperty( override val session: FirSession, override val returnTypeRef: FirTypeRef, override val name: Name, @@ -37,20 +37,6 @@ class FirSyntheticProperty private constructor( symbol.bind(this) } - constructor( - session: FirSession, - name: Name, - symbol: FirAccessorSymbol, - delegateGetter: FirSimpleFunction, - delegateSetter: FirSimpleFunction? = null - ) : this( - session, delegateGetter.returnTypeRef, name, isVar = delegateSetter != null, symbol = symbol, - status = FirDeclarationStatusImpl(delegateGetter.visibility, delegateGetter.modality), - resolvePhase = delegateGetter.resolvePhase, - getter = FirSyntheticPropertyAccessor(delegateGetter, isGetter = true), - setter = delegateSetter?.let { FirSyntheticPropertyAccessor(it, isGetter = false) } - ) - override val source: FirSourceElement? get() = null diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyBuilder.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyBuilder.kt new file mode 100644 index 00000000000..b9a965ac31a --- /dev/null +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/synthetic/FirSyntheticPropertyBuilder.kt @@ -0,0 +1,33 @@ +/* + * 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.declarations.FirSimpleFunction +import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl +import org.jetbrains.kotlin.fir.declarations.modality +import org.jetbrains.kotlin.fir.declarations.visibility +import org.jetbrains.kotlin.fir.symbols.impl.FirAccessorSymbol +import org.jetbrains.kotlin.name.Name + +class FirSyntheticPropertyBuilder { + lateinit var session: FirSession + lateinit var name: Name + lateinit var symbol: FirAccessorSymbol + lateinit var delegateGetter: FirSimpleFunction + var delegateSetter: FirSimpleFunction? = null + + fun build(): FirSyntheticProperty = FirSyntheticProperty( + session, delegateGetter.returnTypeRef, name, isVar = delegateSetter != null, symbol = symbol, + status = FirDeclarationStatusImpl(delegateGetter.visibility, delegateGetter.modality), + resolvePhase = delegateGetter.resolvePhase, + getter = FirSyntheticPropertyAccessor(delegateGetter, isGetter = true), + setter = delegateSetter?.let { FirSyntheticPropertyAccessor(it, isGetter = false) } + ) +} + +fun buildSyntheticProperty(f: FirSyntheticPropertyBuilder.() -> Unit): FirSyntheticProperty = + FirSyntheticPropertyBuilder().apply { f() }.build() \ No newline at end of file