From 2437e8a06cd02331d6564bc7138c359755682f8d Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Mon, 3 Oct 2022 14:43:48 +0300 Subject: [PATCH] [K/N] Extract descriptors search from Context into a DescriptorsLookup Reduces dependency on Context class in Kotlin/Native backend. --- .../jetbrains/kotlin/backend/konan/Context.kt | 15 --- .../kotlin/backend/konan/DescriptorsLookup.kt | 32 +++++++ .../jetbrains/kotlin/backend/konan/PsiToIr.kt | 3 +- .../jetbrains/kotlin/backend/konan/ir/Ir.kt | 95 +++++++++---------- 4 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DescriptorsLookup.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt index 016120ef4f9..339a346ec2e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt @@ -99,15 +99,6 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) { val phaseConfig = config.phaseConfig - private val packageScope by lazy { builtIns.builtInsModule.getPackage(KonanFqNames.internalPackageName).memberScope } - - val nativePtr by lazy { packageScope.getContributedClassifier(NATIVE_PTR_NAME) as ClassDescriptor } - val nonNullNativePtr by lazy { packageScope.getContributedClassifier(NON_NULL_NATIVE_PTR_NAME) as ClassDescriptor } - val getNativeNullPtr by lazy { packageScope.getContributedFunctions("getNativeNullPtr").single() } - val immutableBlobOf by lazy { - builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBlobOf").single() - } - val innerClassesSupport by lazy { InnerClassesSupport(mapping, irFactory) } val bridgesSupport by lazy { BridgesSupport(mapping, irBuiltIns, irFactory) } val inlineFunctionsSupport by lazy { InlineFunctionsSupport(mapping) } @@ -330,12 +321,6 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) { } } -private fun MemberScope.getContributedClassifier(name: String) = - this.getContributedClassifier(Name.identifier(name), NoLookupLocation.FROM_BUILTINS) - -private fun MemberScope.getContributedFunctions(name: String) = - this.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BUILTINS) - internal class ContextLogger(val context: LoggingContext) { operator fun String.unaryPlus() = context.log { this } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DescriptorsLookup.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DescriptorsLookup.kt new file mode 100644 index 00000000000..8ad5006bf05 --- /dev/null +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/DescriptorsLookup.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2022 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.konan + +import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.scopes.MemberScope + +internal class DescriptorsLookup(val builtIns: KonanBuiltIns) { + private val packageScope by lazy { builtIns.builtInsModule.getPackage(KonanFqNames.internalPackageName).memberScope } + + val nativePtr by lazy { packageScope.getContributedClassifier(NATIVE_PTR_NAME) as ClassDescriptor } + val getNativeNullPtr by lazy { packageScope.getContributedFunctions("getNativeNullPtr").single() } + val immutableBlobOf by lazy { + builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBlobOf").single() + } + + val interopBuiltIns by lazy { + InteropBuiltIns(this.builtIns) + } + + private fun MemberScope.getContributedClassifier(name: String) = + this.getContributedClassifier(Name.identifier(name), NoLookupLocation.FROM_BUILTINS) + + private fun MemberScope.getContributedFunctions(name: String) = + this.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BUILTINS) +} \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/PsiToIr.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/PsiToIr.kt index cdd294cb722..6a671b7a51f 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/PsiToIr.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/PsiToIr.kt @@ -82,7 +82,8 @@ internal fun Context.psiToIr( else LazyIrFunctionFactory(symbolTable, stubGenerator, irBuiltInsOverDescriptors, reflectionTypes) irBuiltInsOverDescriptors.functionFactory = functionIrClassFactory - val symbols = KonanSymbols(this, generatorContext.irBuiltIns, symbolTable, symbolTable.lazyWrapper) + val descriptorsLookup = DescriptorsLookup(this.builtIns) + val symbols = KonanSymbols(this, descriptorsLookup, generatorContext.irBuiltIns, symbolTable, symbolTable.lazyWrapper) val irDeserializer = if (isProducingLibrary && !useLinkerWhenProducingLibrary) { // Enable lazy IR generation for newly-created symbols inside BE diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt index 33953c3304c..9bc90153616 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt @@ -40,6 +40,7 @@ internal class KonanIr(context: Context, irModule: IrModuleFragment): Ir