From 345152d198adf098a29bd844751b2c4627e2a870 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Thu, 8 Jul 2021 22:31:59 +0300 Subject: [PATCH] [FIR] Add containingDeclarationSymbol to FirTypeParameter --- .../deserialization/FirMemberDeserializer.kt | 33 +++++--- .../deserialization/FirTypeDeserializer.kt | 5 +- .../impl/FirBuiltinSymbolProvider.kt | 2 + .../converter/DeclarationsConverter.kt | 62 +++++++++----- .../kotlin/fir/builder/RawFirBuilder.kt | 80 +++++++++++-------- .../transformers/FirSyntheticCallGenerator.kt | 9 ++- .../fir/declarations/FirTypeParameter.kt | 2 + .../builder/FirTypeParameterBuilder.kt | 3 + .../declarations/impl/FirTypeParameterImpl.kt | 2 + .../fir/tree/generator/NodeConfigurator.kt | 1 + .../tree/generator/printer/implementation.kt | 30 +++---- 11 files changed, 148 insertions(+), 81 deletions(-) diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt index c500a304cdb..b7c15837173 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirMemberDeserializer.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.builder.buildExpressionStub import org.jetbrains.kotlin.fir.resolve.defaultType +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef @@ -62,7 +63,8 @@ class FirDeserializationContext( containerSource: DeserializedContainerSource? = this.containerSource, outerClassSymbol: FirRegularClassSymbol? = this.outerClassSymbol, annotationDeserializer: AbstractAnnotationDeserializer = this.annotationDeserializer, - capturesTypeParameters: Boolean = true + capturesTypeParameters: Boolean = true, + containingDeclarationSymbol: FirBasedSymbol<*>? = this.outerClassSymbol ): FirDeserializationContext = FirDeserializationContext( nameResolver, typeTable, @@ -70,7 +72,15 @@ class FirDeserializationContext( moduleData, packageFqName, relativeClassName, - FirTypeDeserializer(moduleData, nameResolver, typeTable, annotationDeserializer, typeParameterProtos, typeDeserializer), + FirTypeDeserializer( + moduleData, + nameResolver, + typeTable, + annotationDeserializer, + typeParameterProtos, + typeDeserializer, + containingDeclarationSymbol + ), annotationDeserializer, constDeserializer, containerSource, @@ -101,7 +111,8 @@ class FirDeserializationContext( relativeClassName = null, typeParameterProtos = emptyList(), containerSource, - outerClassSymbol = null + outerClassSymbol = null, + containingDeclarationSymbol = null ) fun createForClass( @@ -124,6 +135,7 @@ class FirDeserializationContext( classId.relativeClassName, classProto.typeParameterList, containerSource, + outerClassSymbol, outerClassSymbol ) @@ -139,6 +151,7 @@ class FirDeserializationContext( typeParameterProtos: List, containerSource: DeserializedContainerSource?, outerClassSymbol: FirRegularClassSymbol?, + containingDeclarationSymbol: FirBasedSymbol<*>? ): FirDeserializationContext { return FirDeserializationContext( nameResolver, typeTable, @@ -152,7 +165,8 @@ class FirDeserializationContext( typeTable, annotationDeserializer, typeParameterProtos, - null + null, + containingDeclarationSymbol ), annotationDeserializer, constDeserializer, @@ -176,8 +190,9 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { fun loadTypeAlias(proto: ProtoBuf.TypeAlias): FirTypeAlias { val flags = proto.flags val name = c.nameResolver.getName(proto.name) - val local = c.childContext(proto.typeParameterList) val classId = ClassId(c.packageFqName, name) + val symbol = FirTypeAliasSymbol(classId) + val local = c.childContext(proto.typeParameterList, containingDeclarationSymbol = symbol) return buildTypeAlias { moduleData = c.moduleData origin = FirDeclarationOrigin.Library @@ -193,7 +208,7 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { } annotations += c.annotationDeserializer.loadTypeAliasAnnotations(proto, local.nameResolver) - symbol = FirTypeAliasSymbol(classId) + this.symbol = symbol expandedTypeRef = proto.underlyingType(c.typeTable).toTypeRef(local) resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES typeParameters += local.typeDeserializer.ownTypeParameters.map { it.fir } @@ -212,7 +227,7 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { val callableName = c.nameResolver.getName(proto.name) val callableId = CallableId(c.packageFqName, c.relativeClassName, callableName) val symbol = FirPropertySymbol(callableId) - val local = c.childContext(proto.typeParameterList) + val local = c.childContext(proto.typeParameterList, containingDeclarationSymbol = symbol) // Per documentation on Property.getter_flags in metadata.proto, if an accessor flags field is absent, its value should be computed // by taking hasAnnotations/visibility/modality from property flags, and using false for the rest @@ -381,7 +396,7 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { val callableName = c.nameResolver.getName(proto.name) val callableId = CallableId(c.packageFqName, c.relativeClassName, callableName) val symbol = FirNamedFunctionSymbol(callableId) - val local = c.childContext(proto.typeParameterList) + val local = c.childContext(proto.typeParameterList, containingDeclarationSymbol = symbol) val simpleFunction = buildSimpleFunction { moduleData = c.moduleData @@ -443,7 +458,7 @@ class FirMemberDeserializer(private val c: FirDeserializationContext) { val relativeClassName = c.relativeClassName!! val callableId = CallableId(c.packageFqName, relativeClassName, relativeClassName.shortName()) val symbol = FirConstructorSymbol(callableId) - val local = c.childContext(emptyList()) + val local = c.childContext(emptyList(), containingDeclarationSymbol = symbol) val isPrimary = !Flags.IS_SECONDARY.get(flags) val typeParameters = classBuilder.typeParameters diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt index 96752ebf55c..17e59953aea 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/FirTypeDeserializer.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.firUnsafe import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol @@ -43,7 +44,8 @@ class FirTypeDeserializer( val typeTable: TypeTable, val annotationDeserializer: AbstractAnnotationDeserializer, typeParameterProtos: List, - val parent: FirTypeDeserializer? + val parent: FirTypeDeserializer?, + val containingSymbol: FirBasedSymbol<*>? ) { private val typeParameterDescriptors: Map = if (typeParameterProtos.isNotEmpty()) { LinkedHashMap() @@ -73,6 +75,7 @@ class FirTypeDeserializer( origin = FirDeclarationOrigin.Library this.name = name this.symbol = symbol + this.containingDeclarationSymbol = containingSymbol variance = proto.variance.convertVariance() isReified = proto.reified } diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirBuiltinSymbolProvider.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirBuiltinSymbolProvider.kt index 91f9371b5ce..b6502633811 100644 --- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirBuiltinSymbolProvider.kt +++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirBuiltinSymbolProvider.kt @@ -229,6 +229,7 @@ private class SyntheticFunctionalInterfaceCache(private val moduleData: FirModul origin = FirDeclarationOrigin.BuiltIns name = Name.identifier("P$it") symbol = FirTypeParameterSymbol() + containingDeclarationSymbol = this@symbol variance = Variance.IN_VARIANCE isReified = false bounds += moduleData.session.builtinTypes.nullableAnyType @@ -242,6 +243,7 @@ private class SyntheticFunctionalInterfaceCache(private val moduleData: FirModul origin = FirDeclarationOrigin.BuiltIns name = Name.identifier("R") symbol = FirTypeParameterSymbol() + containingDeclarationSymbol = this@symbol variance = Variance.OUT_VARIANCE isReified = false bounds += moduleData.session.builtinTypes.nullableAnyType diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index 3d18b026fb7..955d0a89d35 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.fir.references.builder.buildExplicitThisReference import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference import org.jetbrains.kotlin.fir.references.impl.FirReferencePlaceholderForResolvedAnnotations import org.jetbrains.kotlin.fir.scopes.FirScopeProvider +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.* @@ -374,8 +375,8 @@ class DeclarationsConverter( val typeConstraints = mutableListOf() var classBody: LighterASTNode? = null var superTypeList: LighterASTNode? = null - var typeParameterList: LighterASTNode? = null + classNode.forEachChildren { when (it.tokenType) { MODIFIER_LIST -> modifiers = convertModifierList(it, isInClass = true) @@ -390,7 +391,6 @@ class DeclarationsConverter( CLASS_BODY -> classBody = it } } - typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints) } if (classKind == ClassKind.CLASS) { classKind = when { @@ -418,6 +418,10 @@ class DeclarationsConverter( isExternal = modifiers.hasExternal() } + val classSymbol = FirRegularClassSymbol(context.currentClassId) + + typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints, classSymbol) } + withCapturedTypeParameters(status.isInner, firTypeParameters) { buildRegularClass { source = classNode.toFirSourceElement() @@ -427,7 +431,7 @@ class DeclarationsConverter( this.status = status this.classKind = classKind scopeProvider = baseScopeProvider - symbol = FirRegularClassSymbol(context.currentClassId) + symbol = classSymbol annotations += modifiers.annotations typeParameters += firTypeParameters @@ -565,7 +569,8 @@ class DeclarationsConverter( scopeProvider = baseScopeProvider symbol = FirAnonymousObjectSymbol() context.applyToActualCapturedTypeParameters(false) { - typeParameters += buildOuterClassTypeParameterRef { this.symbol = it } } + typeParameters += buildOuterClassTypeParameterRef { this.symbol = it } + } val delegatedSelfType = objectLiteral.toDelegatedSelfType(this) registerSelfType(delegatedSelfType) @@ -958,18 +963,26 @@ class DeclarationsConverter( var modifiers = Modifier() var identifier: String? = null lateinit var firType: FirTypeRef - val firTypeParameters = mutableListOf() + typeAlias.forEachChildren { when (it.tokenType) { MODIFIER_LIST -> modifiers = convertModifierList(it) IDENTIFIER -> identifier = it.asText - TYPE_PARAMETER_LIST -> firTypeParameters += convertTypeParameters(it, emptyList()) TYPE_REFERENCE -> firType = convertType(it) } } val typeAliasName = identifier.nameAsSafeName() return withChildClassName(typeAliasName) { + val classSymbol = FirTypeAliasSymbol(context.currentClassId) + + val firTypeParameters = mutableListOf() + typeAlias.forEachChildren { + if (it.tokenType == TYPE_PARAMETER_LIST) { + firTypeParameters += convertTypeParameters(it, emptyList(), classSymbol) + } + } + return@withChildClassName buildTypeAlias { source = typeAlias.toFirSourceElement() moduleData = baseModuleData @@ -979,7 +992,7 @@ class DeclarationsConverter( isExpect = modifiers.hasExpect() isActual = modifiers.hasActual() } - symbol = FirTypeAliasSymbol(context.currentClassId) + symbol = classSymbol expandedTypeRef = firType annotations += modifiers.annotations typeParameters += firTypeParameters @@ -1020,8 +1033,6 @@ class DeclarationsConverter( } } - typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints) } - val propertyName = identifier.nameAsSafeName() val parentNode = property.getParent() @@ -1042,9 +1053,12 @@ class DeclarationsConverter( (it.getExpressionInParentheses() ?: it).toFirSourceElement() } + symbol = if (isLocal) FirPropertySymbol(propertyName) else FirPropertySymbol(callableIdForName(propertyName)) + + typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints, symbol) } + if (isLocal) { this.isLocal = true - symbol = FirPropertySymbol(propertyName) val delegateBuilder = delegateExpression?.let { FirWrappedDelegateExpressionBuilder().apply { source = delegateSource @@ -1071,7 +1085,6 @@ class DeclarationsConverter( } else { this.isLocal = false receiverTypeRef = receiverType - symbol = FirPropertySymbol(callableIdForName(propertyName)) dispatchReceiverType = currentDispatchReceiverType() withCapturedTypeParameters(true, firTypeParameters) { typeParameters += firTypeParameters @@ -1353,7 +1366,6 @@ class DeclarationsConverter( fun convertFunctionDeclaration(functionDeclaration: LighterASTNode, classWrapper: ClassWrapper? = null): FirStatement { var modifiers = Modifier() var identifier: String? = null - val firTypeParameters = mutableListOf() var valueParametersList: LighterASTNode? = null var isReturnType = false var receiverType: FirTypeRef? = null @@ -1379,7 +1391,6 @@ class DeclarationsConverter( else -> if (it.isExpression()) expression = it } } - typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints) } if (returnType == null) { returnType = @@ -1391,19 +1402,22 @@ class DeclarationsConverter( val isLocal = !(parentNode?.tokenType == KT_FILE || parentNode?.tokenType == CLASS_BODY) val target: FirFunctionTarget val functionSource = functionDeclaration.toFirSourceElement() + val functionSymbol: FirFunctionSymbol<*> val functionBuilder = if (identifier == null && isLocal) { val labelName = functionDeclaration.getLabelName() ?: context.calleeNamesForLambda.lastOrNull()?.identifier target = FirFunctionTarget(labelName = labelName, isLambda = false) + functionSymbol = FirAnonymousFunctionSymbol() FirAnonymousFunctionBuilder().apply { source = functionSource receiverTypeRef = receiverType - symbol = FirAnonymousFunctionSymbol() + symbol = functionSymbol isLambda = false } } else { val functionName = identifier.nameAsSafeName() val labelName = runIf(!functionName.isSpecial) { functionName.identifier } target = FirFunctionTarget(labelName, isLambda = false) + functionSymbol = FirNamedFunctionSymbol(callableIdForName(functionName)) FirSimpleFunctionBuilder().apply { source = functionSource receiverTypeRef = receiverType @@ -1423,11 +1437,14 @@ class DeclarationsConverter( isSuspend = modifiers.hasSuspend() } - symbol = FirNamedFunctionSymbol(callableIdForName(functionName)) + symbol = functionSymbol dispatchReceiverType = currentDispatchReceiverType() } } + val firTypeParameters = mutableListOf() + typeParameterList?.let { firTypeParameters += convertTypeParameters(it, typeConstraints, functionSymbol) } + val function = functionBuilder.apply { moduleData = baseModuleData origin = FirDeclarationOrigin.Source @@ -1627,10 +1644,14 @@ class DeclarationsConverter( /** * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeParameterList */ - private fun convertTypeParameters(typeParameterList: LighterASTNode, typeConstraints: List): List { + private fun convertTypeParameters( + typeParameterList: LighterASTNode, + typeConstraints: List, + containingDeclarationSymbol: FirBasedSymbol<*> + ): List { return typeParameterList.forEachChildrenReturnList { node, container -> when (node.tokenType) { - TYPE_PARAMETER -> container += convertTypeParameter(node, typeConstraints) + TYPE_PARAMETER -> container += convertTypeParameter(node, typeConstraints, containingDeclarationSymbol) } } } @@ -1672,7 +1693,11 @@ class DeclarationsConverter( /** * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeParameter */ - private fun convertTypeParameter(typeParameter: LighterASTNode, typeConstraints: List): FirTypeParameter { + private fun convertTypeParameter( + typeParameter: LighterASTNode, + typeConstraints: List, + containingSymbol: FirBasedSymbol<*> + ): FirTypeParameter { var typeParameterModifiers = TypeParameterModifier() var identifier: String? = null var firType: FirTypeRef? = null @@ -1690,6 +1715,7 @@ class DeclarationsConverter( origin = FirDeclarationOrigin.Source name = identifier.nameAsSafeName() symbol = FirTypeParameterSymbol() + containingDeclarationSymbol = containingSymbol variance = typeParameterModifiers.getVariance() isReified = typeParameterModifiers.hasReified() annotations += typeParameterModifiers.annotations diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 9f8b77a3f88..083d7be1a1f 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.fir.expressions.builder.* import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock import org.jetbrains.kotlin.fir.references.builder.* import org.jetbrains.kotlin.fir.scopes.FirScopeProvider +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.* @@ -532,18 +533,55 @@ open class RawFirBuilder( extractAnnotationsTo(container.annotations) } - private fun KtTypeParameterListOwner.extractTypeParametersTo(container: FirTypeParameterRefsOwnerBuilder) { + private fun KtTypeParameterListOwner.extractTypeParametersTo( + container: FirTypeParameterRefsOwnerBuilder, + declarationSymbol: FirBasedSymbol<*> + ) { for (typeParameter in typeParameters) { - container.typeParameters += typeParameter.convert() + container.typeParameters += extractTypeParameter(typeParameter, declarationSymbol) } } - private fun KtTypeParameterListOwner.extractTypeParametersTo(container: FirTypeParametersOwnerBuilder) { + private fun KtTypeParameterListOwner.extractTypeParametersTo( + container: FirTypeParametersOwnerBuilder, + declarationSymbol: FirBasedSymbol<*> + ) { for (typeParameter in typeParameters) { - container.typeParameters += typeParameter.convert() + container.typeParameters += extractTypeParameter(typeParameter, declarationSymbol) } } + private fun extractTypeParameter(parameter: KtTypeParameter, declarationSymbol: FirBasedSymbol<*>): FirTypeParameter { + val parameterName = parameter.nameAsSafeName + return buildTypeParameter { + source = parameter.toFirSourceElement() + moduleData = baseModuleData + origin = FirDeclarationOrigin.Source + name = parameterName + symbol = FirTypeParameterSymbol() + containingDeclarationSymbol = declarationSymbol + variance = parameter.variance + isReified = parameter.hasModifier(REIFIED_KEYWORD) + parameter.extractAnnotationsTo(this) + val extendsBound = parameter.extendsBound + if (extendsBound != null) { + bounds += extendsBound.convert() + } + val owner = parameter.getStrictParentOfType() ?: return@buildTypeParameter + for (typeConstraint in owner.typeConstraints) { + val subjectName = typeConstraint.subjectTypeParameterName?.getReferencedNameAsName() + if (subjectName == parameterName) { + bounds += typeConstraint.boundTypeReference.toFirOrErrorType() + } + } + addDefaultBoundIfNecessary() + } + } + + override fun visitTypeParameter(parameter: KtTypeParameter, data: Unit?): FirElement { + throw AssertionError("KtTypeParameter should be process via extractTypeParameter") + } + private fun KtTypeParameterListOwner.fillDanglingConstraintsTo(to: T) where T : FirDeclaration, T : FirTypeParameterRefsOwner { val typeParamNames = typeParameters.mapNotNull { it.nameAsName }.toSet() val result = typeConstraints.mapNotNull { constraint -> @@ -890,7 +928,7 @@ open class RawFirBuilder( symbol = FirRegularClassSymbol(context.currentClassId) classOrObject.extractAnnotationsTo(this) - classOrObject.extractTypeParametersTo(this) + classOrObject.extractTypeParametersTo(this, symbol) context.applyToActualCapturedTypeParameters(true) { typeParameters += buildOuterClassTypeParameterRef { symbol = it } @@ -1032,7 +1070,7 @@ open class RawFirBuilder( symbol = FirTypeAliasSymbol(context.currentClassId) expandedTypeRef = typeAlias.getTypeReference().toFirOrErrorType() typeAlias.extractAnnotationsTo(this) - typeAlias.extractTypeParametersTo(this) + typeAlias.extractTypeParametersTo(this, symbol) } } } @@ -1090,7 +1128,7 @@ open class RawFirBuilder( context.firFunctionTargets += target function.extractAnnotationsTo(this) if (this is FirSimpleFunctionBuilder) { - function.extractTypeParametersTo(this) + function.extractTypeParametersTo(this, symbol) } for (valueParameter in function.valueParameters) { valueParameters += valueParameter.convert() @@ -1373,7 +1411,7 @@ open class RawFirBuilder( receiverTypeRef = receiverTypeReference.convertSafe() symbol = FirPropertySymbol(callableIdForName(propertyName)) dispatchReceiverType = currentDispatchReceiverType() - extractTypeParametersTo(this) + extractTypeParametersTo(this, symbol) withCapturedTypeParameters(true, this.typeParameters) { val delegateBuilder = if (hasDelegate()) { FirWrappedDelegateExpressionBuilder().apply { @@ -1557,32 +1595,6 @@ open class RawFirBuilder( } } - override fun visitTypeParameter(parameter: KtTypeParameter, data: Unit): FirElement { - val parameterName = parameter.nameAsSafeName - return buildTypeParameter { - source = parameter.toFirSourceElement() - moduleData = baseModuleData - origin = FirDeclarationOrigin.Source - name = parameterName - symbol = FirTypeParameterSymbol() - variance = parameter.variance - isReified = parameter.hasModifier(REIFIED_KEYWORD) - parameter.extractAnnotationsTo(this) - val extendsBound = parameter.extendsBound - if (extendsBound != null) { - bounds += extendsBound.convert() - } - val owner = parameter.getStrictParentOfType() ?: return@buildTypeParameter - for (typeConstraint in owner.typeConstraints) { - val subjectName = typeConstraint.subjectTypeParameterName?.getReferencedNameAsName() - if (subjectName == parameterName) { - bounds += typeConstraint.boundTypeReference.toFirOrErrorType() - } - } - addDefaultBoundIfNecessary() - } - } - // TODO introduce placeholder projection type private fun KtTypeProjection.isPlaceholderProjection() = projectionKind == KtProjectionKind.NONE && (typeReference?.typeElement as? KtUserType)?.referencedName == "_" diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt index 55afb709455..22a4fb19f88 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt @@ -218,7 +218,7 @@ class FirSyntheticCallGenerator( containingDeclarations = components.containingDeclarations ) - private fun generateSyntheticSelectTypeParameter(): Pair { + private fun generateSyntheticSelectTypeParameter(functionSymbol: FirSyntheticFunctionSymbol): Pair { val typeParameterSymbol = FirTypeParameterSymbol() val typeParameter = buildTypeParameter { @@ -227,6 +227,7 @@ class FirSyntheticCallGenerator( resolvePhase = FirResolvePhase.ANALYZED_DEPENDENCIES name = Name.identifier("K") symbol = typeParameterSymbol + containingDeclarationSymbol = functionSymbol variance = Variance.INVARIANT isReified = false addDefaultBoundIfNecessary() @@ -242,7 +243,7 @@ class FirSyntheticCallGenerator( // fun select(vararg values: K): K val functionSymbol = FirSyntheticFunctionSymbol(callableId) - val (typeParameter, returnType) = generateSyntheticSelectTypeParameter() + val (typeParameter, returnType) = generateSyntheticSelectTypeParameter(functionSymbol) val argumentType = buildResolvedTypeRef { type = returnType.type.createArrayType() } val typeArgument = buildTypeProjectionWithVariance { @@ -264,7 +265,7 @@ class FirSyntheticCallGenerator( // fun test(a: X) = a!! // `X` is not a subtype of `Any` and hence cannot satisfy `K` if it had an upper bound of `Any`. val functionSymbol = FirSyntheticFunctionSymbol(SyntheticCallableId.CHECK_NOT_NULL) - val (typeParameter, returnType) = generateSyntheticSelectTypeParameter() + val (typeParameter, returnType) = generateSyntheticSelectTypeParameter(functionSymbol) val argumentType = buildResolvedTypeRef { type = returnType.type.withNullability(ConeNullability.NULLABLE, session.typeContext) @@ -292,7 +293,7 @@ class FirSyntheticCallGenerator( // fun test(a: X, b: X) = a ?: b // `X` is not a subtype of `Any` and hence cannot satisfy `K` if it had an upper bound of `Any`. val functionSymbol = FirSyntheticFunctionSymbol(SyntheticCallableId.ELVIS_NOT_NULL) - val (typeParameter, rightArgumentType) = generateSyntheticSelectTypeParameter() + val (typeParameter, rightArgumentType) = generateSyntheticSelectTypeParameter(functionSymbol) val leftArgumentType = buildResolvedTypeRef { type = rightArgumentType.coneTypeUnsafe().withNullability(ConeNullability.NULLABLE, session.typeContext) diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/FirTypeParameter.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/FirTypeParameter.kt index c74bdd015e0..89b3dc6cf90 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/FirTypeParameter.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/FirTypeParameter.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.FirModuleData import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.name.Name @@ -28,6 +29,7 @@ abstract class FirTypeParameter : FirTypeParameterRef, FirAnnotatedDeclaration() abstract override val attributes: FirDeclarationAttributes abstract val name: Name abstract override val symbol: FirTypeParameterSymbol + abstract val containingDeclarationSymbol: FirBasedSymbol<*>? abstract val variance: Variance abstract val isReified: Boolean abstract val bounds: List diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirTypeParameterBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirTypeParameterBuilder.kt index 7c521776456..dc071c40ae3 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirTypeParameterBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirTypeParameterBuilder.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirTypeParameter import org.jetbrains.kotlin.fir.declarations.impl.FirTypeParameterImpl import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.visitors.* @@ -36,6 +37,7 @@ class FirTypeParameterBuilder : FirAnnotationContainerBuilder { var attributes: FirDeclarationAttributes = FirDeclarationAttributes() lateinit var name: Name lateinit var symbol: FirTypeParameterSymbol + var containingDeclarationSymbol: FirBasedSymbol<*>? = null lateinit var variance: Variance var isReified: Boolean by kotlin.properties.Delegates.notNull() val bounds: MutableList = mutableListOf() @@ -50,6 +52,7 @@ class FirTypeParameterBuilder : FirAnnotationContainerBuilder { attributes, name, symbol, + containingDeclarationSymbol, variance, isReified, bounds, diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt index 800bf4b18b7..1581d87eb78 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirTypeParameter import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.name.Name @@ -31,6 +32,7 @@ internal class FirTypeParameterImpl( override val attributes: FirDeclarationAttributes, override val name: Name, override val symbol: FirTypeParameterSymbol, + override val containingDeclarationSymbol: FirBasedSymbol<*>?, override val variance: Variance, override val isReified: Boolean, override val bounds: MutableList, diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt index 8ecfb9f486b..83c3826d821 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt @@ -289,6 +289,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild typeParameter.configure { +name +symbol("FirTypeParameterSymbol") + +field("containingDeclarationSymbol", firBasedSymbolType, "*", nullable = true) +field(varianceType) +booleanField("isReified") +fieldList("bounds", typeRef, withReplace = true) diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/printer/implementation.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/printer/implementation.kt index 5e1e2fb84fa..090d9bcc9ff 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/printer/implementation.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/printer/implementation.kt @@ -102,23 +102,23 @@ fun SmartPrinter.printImplementation(implementation: Implementation) { } - element.allFields.filter { it.type.contains("Symbol") && it !is FieldList } - .takeIf { - it.isNotEmpty() && !isInterface && !isAbstract && - !element.type.contains("Reference") - && !element.type.contains("ResolvedQualifier") - && !element.type.endsWith("Ref") - } - ?.let { symbolFields -> - println("init {") - for (symbolField in symbolFields) { - withIndent { - println("${symbolField.name}${symbolField.call()}bind(this)") - } + element.allFields.filter { + it.name != "containingDeclarationSymbol" && it.type.contains("Symbol") && it !is FieldList + }.takeIf { + it.isNotEmpty() && !isInterface && !isAbstract && + !element.type.contains("Reference") + && !element.type.contains("ResolvedQualifier") + && !element.type.endsWith("Ref") + }?.let { symbolFields -> + println("init {") + for (symbolField in symbolFields) { + withIndent { + println("${symbolField.name}${symbolField.call()}bind(this)") } - println("}") - println() } + println("}") + println() + } fun Field.acceptString(): String = "${name}${call()}accept(visitor, data)" if (!isInterface && !isAbstract) {