From 6e2e6794aa268a8024929fa5c2ebb8560c2d44b7 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 17 Oct 2018 15:26:21 +0200 Subject: [PATCH] Remove unnecessary copy of ReflectionTypes ReflectionTypes was copied from org.jetbrains.kotlin.builtins to kotlin-native in https://github.com/JetBrains/kotlin-native/commit/776f4c4b7, and then was incorrectly copied back in 27365dc4be. This class is in fact only needed in kotlin-native, where it was moved and simplified in https://github.com/JetBrains/kotlin-native/commit/c3e921d53 --- .../backend/common/CommonBackendContext.kt | 90 ++----------------- .../ir/backend/js/JsIrBackendContext.kt | 9 -- .../kotlin/backend/jvm/JvmBackendContext.kt | 9 +- 3 files changed, 10 insertions(+), 98 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CommonBackendContext.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CommonBackendContext.kt index ff019e893a4..be354761ce8 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CommonBackendContext.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CommonBackendContext.kt @@ -1,28 +1,18 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.common import org.jetbrains.kotlin.backend.common.ir.Ir -import org.jetbrains.kotlin.builtins.KOTLIN_REFLECT_FQ_NAME import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.KotlinTypeFactory -import org.jetbrains.kotlin.types.TypeProjection -import org.jetbrains.kotlin.types.typeUtil.asTypeProjection -import org.jetbrains.kotlin.utils.addIfNotNull -import java.util.* -import kotlin.reflect.KProperty - interface CommonBackendContext : BackendContext { - override val ir: Ir //TODO move to builtins @@ -33,77 +23,7 @@ interface CommonBackendContext : BackendContext { //TODO move to builtins fun getInternalFunctions(name: String): List - val reflectionTypes: ReflectionTypes - fun log(message: () -> String) fun report(element: IrElement?, irFile: IrFile?, message: String, isError: Boolean) } - -class ReflectionTypes(module: ModuleDescriptor, internalPackage: FqName) { - - private val kotlinReflectScope: MemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) { - module.getPackage(KOTLIN_REFLECT_FQ_NAME).memberScope - } - - private val internalScope: MemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) { - module.getPackage(internalPackage).memberScope - } - - private fun find(memberScope: MemberScope, className: String): ClassDescriptor { - val name = Name.identifier(className) - return memberScope.getContributedClassifier(name, NoLookupLocation.FROM_REFLECTION) as ClassDescriptor - } - - private class ClassLookup(val memberScope: MemberScope) { - operator fun getValue(types: ReflectionTypes, property: KProperty<*>): ClassDescriptor { - return types.find(memberScope, property.name.capitalize()) - } - } - - private fun getFunctionTypeArgumentProjections( - receiverType: KotlinType?, - parameterTypes: List, - returnType: KotlinType - ): List { - val arguments = ArrayList(parameterTypes.size + (if (receiverType != null) 1 else 0) + 1) - - arguments.addIfNotNull(receiverType?.asTypeProjection()) - - parameterTypes.mapTo(arguments, KotlinType::asTypeProjection) - - arguments.add(returnType.asTypeProjection()) - - return arguments - } - - fun getKFunction(n: Int): ClassDescriptor = find(kotlinReflectScope, "KFunction$n") - - val kClass: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kProperty0: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kProperty1: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kProperty2: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kMutableProperty0: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kMutableProperty1: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kMutableProperty2: ClassDescriptor by ClassLookup(kotlinReflectScope) - val kFunctionImpl: ClassDescriptor by ClassLookup(internalScope) - val kProperty0Impl: ClassDescriptor by ClassLookup(internalScope) - val kProperty1Impl: ClassDescriptor by ClassLookup(internalScope) - val kProperty2Impl: ClassDescriptor by ClassLookup(internalScope) - val kMutableProperty0Impl: ClassDescriptor by ClassLookup(internalScope) - val kMutableProperty1Impl: ClassDescriptor by ClassLookup(internalScope) - val kMutableProperty2Impl: ClassDescriptor by ClassLookup(internalScope) - val kLocalDelegatedPropertyImpl: ClassDescriptor by ClassLookup(internalScope) - val kLocalDelegatedMutablePropertyImpl: ClassDescriptor by ClassLookup(internalScope) - - fun getKFunctionType( - annotations: Annotations, - receiverType: KotlinType?, - parameterTypes: List, - returnType: KotlinType - ): KotlinType { - val arguments = getFunctionTypeArgumentProjections(receiverType, parameterTypes, returnType) - val classDescriptor = getKFunction(arguments.size - 1 /* return type */) - return KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments) - } -} \ No newline at end of file diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt index 34a8d58762e..a889f3e73e3 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.ir.backend.js import org.jetbrains.kotlin.backend.common.CommonBackendContext -import org.jetbrains.kotlin.backend.common.ReflectionTypes import org.jetbrains.kotlin.backend.common.atMostOne import org.jetbrains.kotlin.backend.common.descriptors.KnownPackageFragmentDescriptor import org.jetbrains.kotlin.backend.common.ir.Ir @@ -78,10 +77,6 @@ class JsIrBackendContext( override val sharedVariablesManager = JsSharedVariablesManager(irBuiltIns, implicitDeclarationFile) override val declarationFactory = JsDeclarationFactory() - override val reflectionTypes: ReflectionTypes by lazy(LazyThreadSafetyMode.PUBLICATION) { - // TODO - ReflectionTypes(module, REFLECT_PACKAGE_FQNAME) - } companion object { val KOTLIN_PACKAGE_FQN = FqName.fromSegments(listOf("kotlin")) @@ -149,10 +144,6 @@ class JsIrBackendContext( val functions = (0..22).map { symbolTable.referenceClass(builtIns.getFunction(it)) } - val kFunctions by lazy { - (0..22).map { symbolTable.referenceClass(reflectionTypes.getKFunction(it)) } - } - val primitiveCompanionObjects = PrimitiveType.NUMBER_TYPES .asSequence() .filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index ddabc708f4a..0bafb07868b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -6,14 +6,15 @@ package org.jetbrains.kotlin.backend.jvm import org.jetbrains.kotlin.backend.common.CommonBackendContext -import org.jetbrains.kotlin.backend.common.ReflectionTypes import org.jetbrains.kotlin.backend.common.ir.Ir import org.jetbrains.kotlin.backend.common.ir.Symbols import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSharedVariablesManager +import org.jetbrains.kotlin.builtins.ReflectionTypes import org.jetbrains.kotlin.codegen.state.GenerationState import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.descriptors.NotFoundClasses import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.IrFile @@ -26,6 +27,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi2ir.PsiSourceManager import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.storage.LockBasedStorageManager class JvmBackendContext( val state: GenerationState, @@ -37,9 +39,8 @@ class JvmBackendContext( override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(psiSourceManager, builtIns, state, symbolTable) override val sharedVariablesManager = JvmSharedVariablesManager(builtIns, irBuiltIns) - override val reflectionTypes: ReflectionTypes by lazy(LazyThreadSafetyMode.PUBLICATION) { - ReflectionTypes(state.module, FqName("kotlin.reflect.jvm.internal")) - } + // TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor + internal val reflectionTypes = ReflectionTypes(state.module, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, state.module)) override val ir = JvmIr(irModuleFragment, symbolTable)