From e5cf7a1be77d3784091bceb25a9caf617355bc0a Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Fri, 16 Oct 2020 14:36:27 +0300 Subject: [PATCH] Remove descriptor usage from Scope.kt --- .../kotlin/backend/common/ir/IrUtils.kt | 13 ---- .../common/lower/inline/FunctionInlining.kt | 5 +- .../kotlin/ir/builders/ExpressionHelpers.kt | 6 +- .../org/jetbrains/kotlin/ir/builders/Scope.kt | 68 +++---------------- .../IrTemporaryVariableDescriptor.kt | 50 -------------- .../backend/ir/SerializerIrGenerator.kt | 8 +-- 6 files changed, 18 insertions(+), 132 deletions(-) delete mode 100644 compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrTemporaryVariableDescriptor.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt index 8b6cf1594fb..1c7108275f2 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt @@ -375,19 +375,6 @@ val IrDeclaration.isTopLevel: Boolean return parentClass?.isFileClass == true && parentClass.parent is IrPackageFragment } -fun Scope.createTemporaryVariableWithWrappedDescriptor( - irExpression: IrExpression, - nameHint: String? = null, - isMutable: Boolean = false, - origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE -): IrVariable { - - val descriptor = WrappedVariableDescriptor() - return createTemporaryVariableWithGivenDescriptor( - irExpression, nameHint, isMutable, origin, descriptor - ).apply { descriptor.bind(this) } -} - fun IrClass.createImplicitParameterDeclarationWithWrappedDescriptor() { thisReceiver = buildReceiverParameter(this, IrDeclarationOrigin.INSTANCE_RECEIVER, symbol.typeWithParameters(typeParameters)) } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt index 269a554903d..99bb1f05b92 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.common.lower.inline import org.jetbrains.kotlin.backend.common.* import org.jetbrains.kotlin.backend.common.ir.Symbols -import org.jetbrains.kotlin.backend.common.ir.createTemporaryVariableWithWrappedDescriptor import org.jetbrains.kotlin.backend.common.ir.getNewWrappedDescriptor import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.config.LanguageFeature @@ -472,7 +471,7 @@ class FunctionInlining( ) } else { val newVariable = - currentScope.scope.createTemporaryVariableWithWrappedDescriptor( + currentScope.scope.createTemporaryVariable( irExpression = it.argumentExpression.transform( // Arguments may reference the previous ones - substitute them. substitutor, data = null @@ -523,7 +522,7 @@ class FunctionInlining( val variableInitializer = argument.argumentExpression.transform(substitutor, data = null) val newVariable = - currentScope.scope.createTemporaryVariableWithWrappedDescriptor( + currentScope.scope.createTemporaryVariable( irExpression = IrBlockImpl( variableInitializer.startOffset, variableInitializer.endOffset, diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt index ac4c18b8f92..9fa53829fea 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt @@ -49,7 +49,7 @@ fun IrStatementsBuilder.irTemporary( typeHint: KotlinType? = null, irType: IrType? = null ): IrVariable { - val temporary = scope.createTemporaryVariable(value, nameHint, type = typeHint, irType = irType) + val temporary = scope.createTemporaryVariable(value, nameHint, irType = irType) +temporary return temporary } @@ -67,9 +67,9 @@ fun IrStatementsBuilder.irTemporaryVarDeclaration( fun IrStatementsBuilder.irTemporaryVar( value: IrExpression, nameHint: String? = null, - typeHint: KotlinType? = null + irType: IrType? = null ): IrVariable { - val temporary = scope.createTemporaryVariable(value, nameHint, isMutable = true, type = typeHint) + val temporary = scope.createTemporaryVariable(value, nameHint, isMutable = true) +temporary return temporary } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt index 5bb4d6dfb17..4755f19430f 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/Scope.kt @@ -24,35 +24,23 @@ import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl -import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptor -import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl +import org.jetbrains.kotlin.ir.descriptors.WrappedVariableDescriptor import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.impl.* import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.impl.originalKotlinType -import org.jetbrains.kotlin.ir.types.toKotlinType import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.types.KotlinType -@OptIn(ObsoleteDescriptorBasedAPI::class) class Scope(val scopeOwnerSymbol: IrSymbol) { - @ObsoleteDescriptorBasedAPI - val scopeOwner: DeclarationDescriptor get() = scopeOwnerSymbol.descriptor - fun getLocalDeclarationParent(): IrDeclarationParent { - if (!scopeOwnerSymbol.isBound) throw AssertionError("Unbound symbol: $scopeOwner") - val scopeOwnerElement = scopeOwnerSymbol.owner - return when (scopeOwnerElement) { + if (!scopeOwnerSymbol.isBound) throw AssertionError("Unbound symbol: $scopeOwnerSymbol") + return when (val scopeOwnerElement = scopeOwnerSymbol.owner) { is IrDeclarationParent -> scopeOwnerElement !is IrDeclaration -> throw AssertionError("Not a declaration: $scopeOwnerElement") else -> scopeOwnerElement.parent } } - @Deprecated("Creates unbound symbol") - constructor(descriptor: DeclarationDescriptor) : this(createSymbolForScopeOwner(descriptor)) - private var lastTemporaryIndex: Int = 0 private fun nextTemporaryIndex(): Int = lastTemporaryIndex++ @@ -61,13 +49,6 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { return if (nameHint != null) "$prefix${index}_$nameHint" else "$prefix$index" } - private fun createDescriptorForTemporaryVariable( - type: KotlinType, - nameHint: String? = null, - isMutable: Boolean = false - ): IrTemporaryVariableDescriptor = - IrTemporaryVariableDescriptorImpl(scopeOwner, Name.identifier(getNameForTemporary(nameHint)), type, isMutable) - private fun getNameForTemporary(nameHint: String?): String = inventNameForTemporary("tmp", nameHint) @@ -75,17 +56,17 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { irType: IrType, nameHint: String? = null, isMutable: Boolean = false, - type: KotlinType? = null, origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE, startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET ): IrVariable { - val originalKotlinType = type ?: irType.toKotlinType() - val descriptor = createDescriptorForTemporaryVariable(originalKotlinType, nameHint, isMutable) + val name = Name.identifier(getNameForTemporary(nameHint)) + val descriptor = WrappedVariableDescriptor() return IrVariableImpl( - startOffset, endOffset, origin, IrVariableSymbolImpl(descriptor), descriptor.name, + startOffset, endOffset, origin, IrVariableSymbolImpl(descriptor), name, irType, isMutable, isConst = false, isLateinit = false ).apply { + descriptor.bind(this) parent = getLocalDeclarationParent() } } @@ -94,46 +75,15 @@ class Scope(val scopeOwnerSymbol: IrSymbol) { irExpression: IrExpression, nameHint: String? = null, isMutable: Boolean = false, - type: KotlinType? = null, origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE, irType: IrType? = null ): IrVariable { - val originalKotlinType = type ?: (irExpression.type.originalKotlinType ?: irExpression.type.toKotlinType()) return createTemporaryVariableDeclaration( irType ?: irExpression.type, - nameHint, isMutable, originalKotlinType, + nameHint, isMutable, origin, irExpression.startOffset, irExpression.endOffset ).apply { initializer = irExpression } } - - fun createTemporaryVariableWithGivenDescriptor( - irExpression: IrExpression, - nameHint: String? = null, - isMutable: Boolean = false, - origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE, - descriptor: VariableDescriptor - ): IrVariable { - return IrVariableImpl( - irExpression.startOffset, irExpression.endOffset, origin, - IrVariableSymbolImpl(descriptor), - Name.identifier(getNameForTemporary(nameHint)), - irExpression.type, - isVar = isMutable, - isConst = false, - isLateinit = false - ).also { - it.initializer = irExpression - } - } -} - -private fun createSymbolForScopeOwner(descriptor: DeclarationDescriptor) = - when (descriptor) { - is ClassDescriptor -> IrClassSymbolImpl(descriptor) - is ClassConstructorDescriptor -> IrConstructorSymbolImpl(descriptor.original) - is FunctionDescriptor -> IrSimpleFunctionSymbolImpl(descriptor.original) - is PropertyDescriptor -> IrFieldSymbolImpl(descriptor) - else -> throw AssertionError("Unexpected scopeOwner descriptor: $descriptor") - } +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrTemporaryVariableDescriptor.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrTemporaryVariableDescriptor.kt deleted file mode 100644 index 240dcbea5bf..00000000000 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/descriptors/IrTemporaryVariableDescriptor.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.ir.descriptors - -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorImpl -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.constants.ConstantValue -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.TypeSubstitutor - -interface IrTemporaryVariableDescriptor : VariableDescriptor - -class IrTemporaryVariableDescriptorImpl( - containingDeclaration: DeclarationDescriptor, - name: Name, - outType: KotlinType, - private val isMutable: Boolean = false -) : VariableDescriptorImpl(containingDeclaration, Annotations.EMPTY, name, outType, SourceElement.NO_SOURCE), - IrTemporaryVariableDescriptor { - override fun getCompileTimeInitializer(): ConstantValue<*>? = null - - override fun getVisibility(): DescriptorVisibility = DescriptorVisibilities.LOCAL - - override fun substitute(substitutor: TypeSubstitutor): VariableDescriptor { - throw UnsupportedOperationException("Temporary variable descriptor shouldn't be substituted (so far): $this") - } - - override fun isVar(): Boolean = isMutable - - override fun isLateInit(): Boolean = false - - override fun accept(visitor: DeclarationDescriptorVisitor, data: D): R = - visitor.visitVariableDescriptor(this, data) -} diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt index 608760c51a0..923b4ac7cb3 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializerIrGenerator.kt @@ -331,7 +331,7 @@ open class SerializerIrGenerator( } // returns null: Any? for boxed types and 0: for primitives - private fun IrBuilderWithScope.defaultValueAndType(prop: SerializableProperty): Pair { + private fun IrBuilderWithScope.defaultValueAndType(prop: SerializableProperty): Pair { val kType = prop.descriptor.returnType!! val T = kType.toIrType() val defaultPrimitive: IrExpression? = when { @@ -346,9 +346,9 @@ open class SerializerIrGenerator( else -> null } return if (defaultPrimitive == null) - irNull(compilerContext.irBuiltIns.anyNType) to (compilerContext.builtIns.nullableAnyType) + irNull(compilerContext.irBuiltIns.anyNType) to (compilerContext.irBuiltIns.anyNType) else - defaultPrimitive to kType + defaultPrimitive to T } override fun generateLoad(function: FunctionDescriptor) = irClass.contributeFunction(function) { loadFunc -> @@ -382,7 +382,7 @@ open class SerializerIrGenerator( irTemporaryVar( expr, "local$i", - typeHint = type + type ) }