From 1f8de46b3c7295926c59ca7f2f7e18a08d72483e Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 22 Feb 2021 10:49:26 +0100 Subject: [PATCH] JVM IR: do not depend on StubGeneratorExtensions in backend Move most of the implementation from JvmGeneratorExtensions to JvmGeneratorExtensionsImpl. The latter implements StubGeneratorExtensions from psi2ir, and doing so helps to remove direct dependency on this part of psi2ir from backend. --- .../compiler/KotlinToJVMBytecodeCompiler.kt | 4 +-- .../backend/jvm/JvmGeneratorExtensions.kt | 16 ++++++++++ .../backend/jvm/JvmGeneratorExtensionsImpl.kt | 30 ++++++------------- .../kotlin/backend/jvm/JvmIrCodegenFactory.kt | 6 ++-- .../kotlin/backend/jvm/JvmSymbols.kt | 14 ++++++--- .../backend/jvm/codegen/AnnotationCodegen.kt | 11 +++---- .../backend/jvm/codegen/irCodegenUtils.kt | 4 +-- .../kotlin/backend/jvm/ir/IrUtils.kt | 2 +- .../jvm/lower/FunctionReferenceLowering.kt | 11 +++++-- .../lower/indy/LambdaMetafactoryArguments.kt | 3 +- .../frontend/fir/Fir2IrResultsConverter.kt | 4 +-- .../kotlin/ir/AbstractIrGeneratorTestCase.kt | 4 +-- .../kotlin/codegen/GenerationUtils.kt | 4 +-- 13 files changed, 65 insertions(+), 48 deletions(-) create mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt index e4dff4e9e99..9643f686163 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt @@ -29,7 +29,7 @@ import org.jetbrains.kotlin.asJava.finder.JavaElementFinder import org.jetbrains.kotlin.backend.common.output.OutputFileCollection import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig -import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions +import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensionsImpl import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory import org.jetbrains.kotlin.backend.jvm.jvmPhases import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys @@ -353,7 +353,7 @@ object KotlinToJVMBytecodeCompiler { performanceManager?.notifyGenerationStarted() performanceManager?.notifyIRTranslationStarted() - val extensions = JvmGeneratorExtensions() + val extensions = JvmGeneratorExtensionsImpl() val (moduleFragment, symbolTable, components) = firAnalyzerFacade.convertToIr(extensions) performanceManager?.notifyIRTranslationFinished() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt new file mode 100644 index 00000000000..d3b64525bca --- /dev/null +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensions.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2021 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.backend.jvm + +import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrConstructor +import org.jetbrains.kotlin.resolve.jvm.JvmClassName + +interface JvmGeneratorExtensions { + val classNameOverride: MutableMap + + val rawTypeAnnotationConstructor: IrConstructor? +} diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt index 13608a5a0dc..e0855a2d60f 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmGeneratorExtensionsImpl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 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. */ @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtPureClassOrObject import org.jetbrains.kotlin.psi.psiUtil.pureEndOffset import org.jetbrains.kotlin.psi.psiUtil.pureStartOffset @@ -49,8 +48,8 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Deserializ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations -class JvmGeneratorExtensions(private val generateFacades: Boolean = true) : GeneratorExtensions() { - val classNameOverride = mutableMapOf() +class JvmGeneratorExtensionsImpl(private val generateFacades: Boolean = true) : GeneratorExtensions(), JvmGeneratorExtensions { + override val classNameOverride: MutableMap = mutableMapOf() override val samConversion: SamConversion get() = JvmSamConversion @@ -162,32 +161,21 @@ class JvmGeneratorExtensions(private val generateFacades: Boolean = true) : Gene } private val flexibleNullabilityAnnotationClass = - createSpecialAnnotationClass(FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME, kotlinIrInternalPackage) + createSpecialAnnotationClass(JvmSymbols.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME, kotlinIrInternalPackage) private val rawTypeAnnotationClass = - createSpecialAnnotationClass(RAW_TYPE_ANNOTATION_FQ_NAME, kotlinIrInternalPackage) + createSpecialAnnotationClass(JvmSymbols.RAW_TYPE_ANNOTATION_FQ_NAME, kotlinIrInternalPackage) // NB Class 'kotlin.jvm.internal.EnhancedNullability' doesn't exist anywhere in descriptors or in bytecode private val enhancedNullabilityAnnotationClass = - createSpecialAnnotationClass(ENHANCED_NULLABILITY_ANNOTATION_FQ_NAME, kotlinJvmInternalPackage) + createSpecialAnnotationClass(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION, kotlinJvmInternalPackage) - override val flexibleNullabilityAnnotationConstructor: IrConstructor? = + override val flexibleNullabilityAnnotationConstructor: IrConstructor = flexibleNullabilityAnnotationClass.constructors.single() - override val enhancedNullabilityAnnotationConstructor: IrConstructor? = + override val enhancedNullabilityAnnotationConstructor: IrConstructor = enhancedNullabilityAnnotationClass.constructors.single() - override val rawTypeAnnotationConstructor: IrConstructor? = + override val rawTypeAnnotationConstructor: IrConstructor = rawTypeAnnotationClass.constructors.single() - - companion object { - val FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME = - IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier("FlexibleNullability")) - - val ENHANCED_NULLABILITY_ANNOTATION_FQ_NAME: FqName = - JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION - - val RAW_TYPE_ANNOTATION_FQ_NAME = - IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier("RawType")) - } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt index 4642f71652a..05b093700f7 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmIrCodegenFactory.kt @@ -60,7 +60,7 @@ class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory @JvmOverloads fun convertToIr(state: GenerationState, files: Collection, ignoreErrors: Boolean = false): JvmIrBackendInput { - val extensions = JvmGeneratorExtensions() + val extensions = JvmGeneratorExtensionsImpl() val mangler = JvmManglerDesc(MainFunctionDetector(state.bindingContext, state.languageVersionSettings)) val psi2ir = Psi2IrTranslator(state.languageVersionSettings, Psi2IrConfiguration(ignoreErrors)) val symbolTable = SymbolTable(JvmIdSignatureDescriptor(mangler), IrFactoryImpl, JvmNameProvider) @@ -217,7 +217,7 @@ class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory state: GenerationState, irModuleFragment: IrModuleFragment, symbolTable: SymbolTable, - extensions: JvmGeneratorExtensions, + extensions: JvmGeneratorExtensionsImpl, backendExtension: JvmBackendExtension, notifyCodegenStart: () -> Unit ) { @@ -232,7 +232,7 @@ class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory fun configureBuiltInsAndGenerateIrProvidersInFrontendIRMode( irModuleFragment: IrModuleFragment, symbolTable: SymbolTable, - extensions: JvmGeneratorExtensions + extensions: JvmGeneratorExtensionsImpl, ): List { irModuleFragment.irBuiltins.functionFactory = IrFunctionFactory(irModuleFragment.irBuiltins, symbolTable) return generateTypicalIrProviderList( diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt index 7bd0daf7d21..23ddede4178 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt @@ -24,9 +24,11 @@ import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrPackageFragment import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl +import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* +import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.JVM_INLINE_ANNOTATION_FQ_NAME @@ -665,10 +667,6 @@ class JvmSymbols( returnType = irBuiltIns.anyType }.symbol - val flexibleNullabilityAnnotationFqName = JvmGeneratorExtensions.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME - val enhancedNullabilityAnnotationFqName = JvmGeneratorExtensions.ENHANCED_NULLABILITY_ANNOTATION_FQ_NAME - val rawTypeAnnotationFQN = JvmGeneratorExtensions.RAW_TYPE_ANNOTATION_FQ_NAME - private val collectionToArrayClass: IrClassSymbol = createClass(FqName("kotlin.jvm.internal.CollectionToArray")) { klass -> klass.origin = JvmLoweredDeclarationOrigin.TO_ARRAY @@ -866,6 +864,14 @@ class JvmSymbols( val runSuspendFunction: IrSimpleFunctionSymbol = kotlinCoroutinesJvmInternalRunSuspendKt.functionByName("runSuspend") + + companion object { + val FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME = + IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier("FlexibleNullability")) + + val RAW_TYPE_ANNOTATION_FQ_NAME = + IrBuiltIns.KOTLIN_INTERNAL_IR_FQN.child(Name.identifier("RawType")) + } } private fun IrClassSymbol.functionByName(name: String): IrSimpleFunctionSymbol = diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt index 6217fba9a38..48eb1079a6c 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt @@ -20,8 +20,8 @@ import org.jetbrains.annotations.NotNull import org.jetbrains.annotations.Nullable import org.jetbrains.kotlin.backend.common.ir.ir2string import org.jetbrains.kotlin.backend.jvm.JvmBackendContext -import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin +import org.jetbrains.kotlin.backend.jvm.JvmSymbols import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.TypeAnnotationCollector @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* +import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker @@ -303,9 +304,9 @@ abstract class AnnotationCodegen( ) internal val internalAnnotations = setOf( - JvmGeneratorExtensions.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME, - JvmGeneratorExtensions.ENHANCED_NULLABILITY_ANNOTATION_FQ_NAME, - JvmGeneratorExtensions.RAW_TYPE_ANNOTATION_FQ_NAME + JvmSymbols.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME, + JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION, + JvmSymbols.RAW_TYPE_ANNOTATION_FQ_NAME ) private fun getRetentionPolicy(irClass: IrClass): RetentionPolicy { @@ -393,7 +394,7 @@ private fun IrClass.getAnnotationRetention(): KotlinRetention? { } private fun IrType.isNullabilityFlexible(): Boolean = - hasAnnotation(JvmGeneratorExtensions.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME) + hasAnnotation(JvmSymbols.FLEXIBLE_NULLABILITY_ANNOTATION_FQ_NAME) // To be generalized to IrMemberAccessExpression as soon as properties get symbols. private fun IrConstructorCall.getValueArgument(name: Name): IrExpression? { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/irCodegenUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/irCodegenUtils.kt index bc1cfda72c5..b2f9b4cb686 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/irCodegenUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/irCodegenUtils.kt @@ -9,8 +9,8 @@ import com.intellij.psi.PsiElement import org.jetbrains.kotlin.backend.common.ir.allOverridden import org.jetbrains.kotlin.backend.common.ir.ir2string import org.jetbrains.kotlin.backend.jvm.JvmBackendContext -import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin +import org.jetbrains.kotlin.backend.jvm.JvmSymbols import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry import org.jetbrains.kotlin.builtins.StandardNames.FqNames import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap @@ -375,7 +375,7 @@ val IrMemberAccessExpression<*>.psiElement: PsiElement? get() = (symbol.descriptor.original as? DeclarationDescriptorWithSource)?.psiElement fun IrSimpleType.isRawType(): Boolean = - hasAnnotation(JvmGeneratorExtensions.RAW_TYPE_ANNOTATION_FQ_NAME) + hasAnnotation(JvmSymbols.RAW_TYPE_ANNOTATION_FQ_NAME) internal fun classFileContainsMethod(classId: ClassId, function: IrFunction, context: JvmBackendContext): Boolean? { val originalSignature = context.methodSignatureMapper.mapSignatureWithGeneric(function).asmMethod diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt index d8b98068b23..bd2b717ee83 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt @@ -386,7 +386,7 @@ fun collectVisibleTypeParameters(scopeOwner: IrTypeParametersContainer): Set JvmIdSignatureDescriptor(JvmManglerDesc(MainFunctionDetector(bindingContext, languageVersionSettings))) } diff --git a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt index b0a3471d9bb..3e603076a5f 100644 --- a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt +++ b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/GenerationUtils.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.TestsCompiletimeError import org.jetbrains.kotlin.analyzer.AnalysisResult import org.jetbrains.kotlin.asJava.finder.JavaElementFinder import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig -import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensions +import org.jetbrains.kotlin.backend.jvm.JvmGeneratorExtensionsImpl import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory import org.jetbrains.kotlin.backend.jvm.jvmPhases import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys @@ -108,7 +108,7 @@ object GenerationUtils { // TODO: add running checkers and check that it's safe to compile val firAnalyzerFacade = FirAnalyzerFacade(session, configuration.languageVersionSettings, files) - val extensions = JvmGeneratorExtensions() + val extensions = JvmGeneratorExtensionsImpl() val (moduleFragment, symbolTable, components) = firAnalyzerFacade.convertToIr(extensions) val dummyBindingContext = NoScopeRecordCliBindingTrace().bindingContext