From 8231377f6ba6183b7325cf127053d92c62d9c213 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 10 Mar 2020 16:52:50 +0300 Subject: [PATCH] FIR2IR: convert qualifiers inside getClass properly --- .../kotlin/fir/backend/Fir2IrVisitor.kt | 65 +++++++++++-------- .../box/annotations/typeAnnotationOnJdk6.kt | 1 - .../java/objectSuperConstructorCall.kt | 1 - .../codegen/box/fullJdk/regressions/kt1770.kt | 1 - .../noDelegationToDefaultMethodInClass.kt | 1 - .../noDelegationToDefaultMethodInInterface.kt | 1 - ...noDelegationToDefaultMethodInInterface2.kt | 1 - .../box/package/privateMembersInImportList.kt | 1 - .../box/reflection/classLiterals/bareArray.kt | 1 - .../box/reflection/mapping/syntheticFields.kt | 1 - .../privateFakeOverrideFromSuperclass.kt | 1 - .../codegen/box/typealias/typeAliasObject.kt | 1 - .../irText/expressions/classReference.fir.txt | 6 +- .../expressions/objectClassReference.fir.txt | 6 +- .../expressions/reflectionLiterals.fir.txt | 3 +- 15 files changed, 43 insertions(+), 48 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 684d6cbbb73..f6ecd616bbd 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -597,6 +597,7 @@ class Fir2IrVisitor( private fun FirQualifiedAccess.findIrReceiver(isDispatch: Boolean): IrExpression? { val firReceiver = if (isDispatch) dispatchReceiver else extensionReceiver + if (firReceiver is FirResolvedQualifier) return firReceiver.toGetObject() return firReceiver.takeIf { it !is FirNoReceiverExpression }?.toIrExpression() ?: explicitReceiver?.toIrExpression() // NB: this applies to the situation when call is unresolved ?: run { @@ -1213,20 +1214,29 @@ class Fir2IrVisitor( } override fun visitGetClassCall(getClassCall: FirGetClassCall, data: Any?): IrElement { + val argument = getClassCall.argument + val irType = getClassCall.typeRef.toIrType() + val irClassType = argument.typeRef.toIrType() + val irClassReferenceSymbol = when (argument) { + is FirResolvedReifiedParameterReference -> { + declarationStorage.getIrTypeParameterSymbol(argument.symbol, ConversionTypeContext.DEFAULT) + } + is FirResolvedQualifier -> { + val symbol = argument.symbol as? FirClassSymbol + ?: return getClassCall.convertWithOffsets { startOffset, endOffset -> + IrErrorCallExpressionImpl( + startOffset, endOffset, irType, "Resolved qualifier ${argument.render()} does not have correct symbol" + ) + } + declarationStorage.getIrClassSymbol(symbol) + } + else -> null + } return getClassCall.convertWithOffsets { startOffset, endOffset -> - val argument = getClassCall.argument - val irType = getClassCall.typeRef.toIrType() - if (argument is FirResolvedReifiedParameterReference) { - IrClassReferenceImpl( - startOffset, endOffset, irType, - declarationStorage.getIrTypeParameterSymbol(argument.symbol, ConversionTypeContext.DEFAULT), - argument.typeRef.toIrType() - ) + if (irClassReferenceSymbol != null) { + IrClassReferenceImpl(startOffset, endOffset, irType, irClassReferenceSymbol, irClassType) } else { - IrGetClassImpl( - startOffset, endOffset, irType, - argument.toIrExpression() - ) + IrGetClassImpl(startOffset, endOffset, irType, argument.toIrExpression()) } } } @@ -1240,25 +1250,26 @@ class Fir2IrVisitor( } } - override fun visitResolvedQualifier(resolvedQualifier: FirResolvedQualifier, data: Any?): IrElement { - val classSymbol = resolvedQualifier.symbol - if (classSymbol != null) { - val resultingSymbol = when (val klass = classSymbol.fir) { - is FirRegularClass -> { - if (klass.classKind in listOf(ClassKind.OBJECT, ClassKind.ENUM_ENTRY)) classSymbol - else klass.companionObject?.symbol ?: classSymbol - } - else -> classSymbol - } - return resolvedQualifier.convertWithOffsets { startOffset, endOffset -> + private fun FirResolvedQualifier.toGetObject(): IrExpression { + val irType = typeRef.toIrType() + val typeRef = typeRef as? FirResolvedTypeRef + val classSymbol = (typeRef?.type as? ConeClassLikeType)?.lookupTag?.toSymbol(session) + return convertWithOffsets { startOffset, endOffset -> + if (classSymbol != null) { IrGetObjectValueImpl( - startOffset, endOffset, - resolvedQualifier.typeRef.toIrType(), - resultingSymbol.toIrSymbol(session, declarationStorage) as IrClassSymbol + startOffset, endOffset, irType, + classSymbol.toIrSymbol(session, declarationStorage) as IrClassSymbol + ) + } else { + IrErrorCallExpressionImpl( + startOffset, endOffset, irType, "Resolved qualifier ${render()} does not have correctly resolved type" ) } } - return visitElement(resolvedQualifier, data) + } + + override fun visitResolvedQualifier(resolvedQualifier: FirResolvedQualifier, data: Any?): IrElement { + return resolvedQualifier.toGetObject() } override fun visitBinaryLogicExpression(binaryLogicExpression: FirBinaryLogicExpression, data: Any?): IrElement { diff --git a/compiler/testData/codegen/box/annotations/typeAnnotationOnJdk6.kt b/compiler/testData/codegen/box/annotations/typeAnnotationOnJdk6.kt index 8d65e282713..0e2d9aea7e6 100644 --- a/compiler/testData/codegen/box/annotations/typeAnnotationOnJdk6.kt +++ b/compiler/testData/codegen/box/annotations/typeAnnotationOnJdk6.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/classLiteral/java/objectSuperConstructorCall.kt b/compiler/testData/codegen/box/classLiteral/java/objectSuperConstructorCall.kt index 076ac39cca3..99eb84cc18a 100644 --- a/compiler/testData/codegen/box/classLiteral/java/objectSuperConstructorCall.kt +++ b/compiler/testData/codegen/box/classLiteral/java/objectSuperConstructorCall.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/fullJdk/regressions/kt1770.kt b/compiler/testData/codegen/box/fullJdk/regressions/kt1770.kt index a7b50523503..5e98b7c40ba 100644 --- a/compiler/testData/codegen/box/fullJdk/regressions/kt1770.kt +++ b/compiler/testData/codegen/box/fullJdk/regressions/kt1770.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // FULL_JDK diff --git a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInClass.kt b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInClass.kt index 4c71cc93760..23a69b0086d 100644 --- a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInClass.kt +++ b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInClass.kt @@ -1,5 +1,4 @@ // !JVM_DEFAULT_MODE: enable -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // JVM_TARGET: 1.8 // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface.kt b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface.kt index c89ba20325b..c9178ca2eeb 100644 --- a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface.kt +++ b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface.kt @@ -1,5 +1,4 @@ // !JVM_DEFAULT_MODE: enable -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // JVM_TARGET: 1.8 // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface2.kt b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface2.kt index 33227fd539c..4687a5d08f2 100644 --- a/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface2.kt +++ b/compiler/testData/codegen/box/jvm8/defaults/noDelegation/noDelegationToDefaultMethodInInterface2.kt @@ -1,5 +1,4 @@ // !JVM_DEFAULT_MODE: enable -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // JVM_TARGET: 1.8 // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/package/privateMembersInImportList.kt b/compiler/testData/codegen/box/package/privateMembersInImportList.kt index 51bd63d7280..e7009753efa 100644 --- a/compiler/testData/codegen/box/package/privateMembersInImportList.kt +++ b/compiler/testData/codegen/box/package/privateMembersInImportList.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR package test import test.C.E1 diff --git a/compiler/testData/codegen/box/reflection/classLiterals/bareArray.kt b/compiler/testData/codegen/box/reflection/classLiterals/bareArray.kt index 1de157fac77..e7b4229df87 100644 --- a/compiler/testData/codegen/box/reflection/classLiterals/bareArray.kt +++ b/compiler/testData/codegen/box/reflection/classLiterals/bareArray.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +BareArrayClassLiteral -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/mapping/syntheticFields.kt b/compiler/testData/codegen/box/reflection/mapping/syntheticFields.kt index 361d5068e7f..0c3ade74956 100644 --- a/compiler/testData/codegen/box/reflection/mapping/syntheticFields.kt +++ b/compiler/testData/codegen/box/reflection/mapping/syntheticFields.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/properties/privateFakeOverrideFromSuperclass.kt b/compiler/testData/codegen/box/reflection/properties/privateFakeOverrideFromSuperclass.kt index 9c3d4735984..d1913ec8c12 100644 --- a/compiler/testData/codegen/box/reflection/properties/privateFakeOverrideFromSuperclass.kt +++ b/compiler/testData/codegen/box/reflection/properties/privateFakeOverrideFromSuperclass.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/typealias/typeAliasObject.kt b/compiler/testData/codegen/box/typealias/typeAliasObject.kt index d5f1caaa072..203f4e5bd5f 100644 --- a/compiler/testData/codegen/box/typealias/typeAliasObject.kt +++ b/compiler/testData/codegen/box/typealias/typeAliasObject.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR object OHolder { val O = "O" } diff --git a/compiler/testData/ir/irText/expressions/classReference.fir.txt b/compiler/testData/ir/irText/expressions/classReference.fir.txt index 2d94afaf6d8..e8bf61b1571 100644 --- a/compiler/testData/ir/irText/expressions/classReference.fir.txt +++ b/compiler/testData/ir/irText/expressions/classReference.fir.txt @@ -20,14 +20,12 @@ FILE fqName: fileName:/classReference.kt $this: VALUE_PARAMETER name: type:kotlin.Any FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY - GET_CLASS type=kotlin.reflect.KClass<.A> - GET_OBJECT 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit + CLASS_REFERENCE 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.reflect.KClass<.A> GET_CLASS type=kotlin.reflect.KClass<.A> CONSTRUCTOR_CALL 'public constructor () [primary] declared in .A' type=.A origin=null CALL 'public final fun (): java.lang.Class> declared in kotlin.jvm' type=java.lang.Class<.A> origin=GET_PROPERTY : .A - $receiver: GET_CLASS type=kotlin.reflect.KClass<.A> - GET_OBJECT 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit + $receiver: CLASS_REFERENCE 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.reflect.KClass<.A> CALL 'public final fun (): java.lang.Class> declared in kotlin.jvm' type=java.lang.Class<.A> origin=GET_PROPERTY : .A $receiver: GET_CLASS type=kotlin.reflect.KClass<.A> diff --git a/compiler/testData/ir/irText/expressions/objectClassReference.fir.txt b/compiler/testData/ir/irText/expressions/objectClassReference.fir.txt index 8ef983e8bcf..224f9cdd1dd 100644 --- a/compiler/testData/ir/irText/expressions/objectClassReference.fir.txt +++ b/compiler/testData/ir/irText/expressions/objectClassReference.fir.txt @@ -20,9 +20,7 @@ FILE fqName: fileName:/objectClassReference.kt $this: VALUE_PARAMETER name: type:kotlin.Any FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY - GET_CLASS type=kotlin.reflect.KClass<.A> - GET_OBJECT 'CLASS OBJECT name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=.A + CLASS_REFERENCE 'CLASS OBJECT name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.reflect.KClass<.A> CALL 'public final fun (): java.lang.Class> declared in kotlin.jvm' type=java.lang.Class<.A> origin=GET_PROPERTY : .A - $receiver: GET_CLASS type=kotlin.reflect.KClass<.A> - GET_OBJECT 'CLASS OBJECT name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=.A + $receiver: CLASS_REFERENCE 'CLASS OBJECT name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.reflect.KClass<.A> diff --git a/compiler/testData/ir/irText/expressions/reflectionLiterals.fir.txt b/compiler/testData/ir/irText/expressions/reflectionLiterals.fir.txt index 57e8c0dbe77..920d1d54df9 100644 --- a/compiler/testData/ir/irText/expressions/reflectionLiterals.fir.txt +++ b/compiler/testData/ir/irText/expressions/reflectionLiterals.fir.txt @@ -35,8 +35,7 @@ FILE fqName: fileName:/reflectionLiterals.kt PROPERTY name:test1 visibility:public modality:FINAL [val] FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.reflect.KClass<.A> visibility:private [final,static] EXPRESSION_BODY - GET_CLASS type=kotlin.reflect.KClass<.A> - GET_OBJECT 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit + CLASS_REFERENCE 'CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.reflect.KClass<.A> FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> () returnType:kotlin.reflect.KClass<.A> correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val] BLOCK_BODY