FIR/Analysis API: Add KtValueParameterSymbol#generatedPrimaryConstructorProperty

This property are used to allow access to the property created from
the primary constructor parameter
This commit is contained in:
Stanislav Erokhin
2022-05-26 11:16:42 +02:00
committed by Space
parent ed881f3f31
commit 2deb0cc237
23 changed files with 97 additions and 3 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisFacade.AnalysisMode
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.KtFe10DescKotlinPropertySymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtType
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.pointers.KtFe10NeverRestoringSymbolPointer
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.base.KtFe10PsiSymbol
@@ -17,8 +18,10 @@ import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtPsiBasedSymbolPointe
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtKotlinPropertySymbol
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.BindingContext
@@ -32,6 +35,13 @@ internal class KtFe10PsiValueParameterSymbol(
bindingContext[BindingContext.VALUE_PARAMETER, psi]
}
override val generatedPrimaryConstructorProperty: KtKotlinPropertySymbol? by cached {
val bindingContext = analysisContext.analyze(psi, AnalysisMode.PARTIAL)
val propertyDescriptor = bindingContext[BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, psi] ?: return@cached null
KtFe10DescKotlinPropertySymbol(propertyDescriptor as PropertyDescriptorImpl, analysisContext)
}
override val hasDefaultValue: Boolean
get() = withValidityAssertion { psi.hasDefaultValue() }
@@ -16,7 +16,9 @@ import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtPsiBasedSymbolPointe
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtKotlinPropertySymbol
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LLFirResolveSession
import org.jetbrains.kotlin.fir.correspondingProperty
import org.jetbrains.kotlin.fir.renderWithType
import org.jetbrains.kotlin.fir.symbols.impl.FirValueParameterSymbol
import org.jetbrains.kotlin.fir.types.arrayElementType
@@ -55,6 +57,15 @@ internal class KtFirValueParameterSymbol(
override val annotationsList by cached { KtFirAnnotationListForDeclaration.create(firSymbol, firResolveSession.useSiteFirSession, token) }
override val generatedPrimaryConstructorProperty: KtKotlinPropertySymbol? by cached {
val propertySymbol = firSymbol.fir.correspondingProperty?.symbol ?: return@cached null
val ktPropertySymbol = builder.variableLikeBuilder.buildPropertySymbol(propertySymbol)
check(ktPropertySymbol is KtKotlinPropertySymbol) {
"Unexpected symbol for primary constructor property ${ktPropertySymbol.javaClass} for fir: ${firSymbol.fir.renderWithType()}"
}
ktPropertySymbol
}
override fun createPointer(): KtSymbolPointer<KtValueParameterSymbol> {
KtPsiBasedSymbolPointer.createForSymbolFromSource(this)?.let { return it }
TODO("Creating pointers for functions parameters from library is not supported yet")
@@ -193,4 +193,9 @@ public abstract class KtValueParameterSymbol : KtVariableLikeSymbol(), KtSymbolW
* The names of the value parameters for `invoke()` are "item" and "p2" (its default parameter name).
*/
abstract override val name: Name
/**
* The corresponding [KtPropertySymbol] if the current value parameter is a `val` or `var` declared inside the primary constructor.
*/
public open val generatedPrimaryConstructorProperty: KtKotlinPropertySymbol? get() = null
}
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/Anno.param1)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -17,6 +18,7 @@ KtValueParameterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/Anno.param2)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -21,6 +21,7 @@ KtPropertyGetterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,2 +1,2 @@
class A() {
class A(val a: Int, b: String) {
}
@@ -1 +1,5 @@
class A
class A {
constructor(a: kotlin.Int, b: kotlin.String)
val a: kotlin.Int
}
@@ -1,3 +1,37 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/A.a)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
isVararg: false
name: a
origin: SOURCE
receiverType: null
returnType: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
isVararg: false
name: b
origin: SOURCE
receiverType: null
returnType: kotlin/String
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtConstructorSymbol:
annotationsList: []
callableIdIfNonLocal: null
@@ -10,7 +44,10 @@ KtConstructorSymbol:
returnType: A
symbolKind: CLASS_MEMBER
typeParameters: []
valueParameters: []
valueParameters: [
KtValueParameterSymbol(a)
KtValueParameterSymbol(b)
]
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
@@ -18,6 +18,7 @@ KtConstructorSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -53,6 +54,7 @@ KtConstructorSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -69,6 +71,7 @@ KtValueParameterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/MyColor.x)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -17,6 +18,7 @@ KtValueParameterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/MyColor.y)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -33,6 +35,7 @@ KtValueParameterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/MyColor.z)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/Style.value)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: KtKotlinPropertySymbol(/Style.value)
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -14,6 +14,7 @@ KtTypeParameterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -61,6 +62,7 @@ KtLocalVariableSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -176,6 +178,7 @@ KtFunctionSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -58,6 +58,7 @@ KtPropertyGetterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -57,6 +57,7 @@ KtPropertyGetterSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -123,6 +123,7 @@ KtNamedClassOrObjectSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -123,6 +123,7 @@ KtNamedClassOrObjectSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -44,6 +45,7 @@ KtFunctionSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -87,6 +89,7 @@ KtFunctionSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -130,6 +133,7 @@ KtFunctionSymbol:
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: false
@@ -1,6 +1,7 @@
KtValueParameterSymbol:
annotationsList: []
callableIdIfNonLocal: null
generatedPrimaryConstructorProperty: null
hasDefaultValue: false
isExtension: false
isImplicitLambdaParameter: true