Remove descriptor usage from Scope.kt

This commit is contained in:
Georgy Bronnikov
2020-10-16 14:36:27 +03:00
parent 8d999a2c13
commit e5cf7a1be7
6 changed files with 18 additions and 132 deletions
@@ -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))
}
@@ -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,
@@ -49,7 +49,7 @@ fun <T : IrElement> IrStatementsBuilder<T>.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 <T : IrElement> IrStatementsBuilder<T>.irTemporaryVarDeclaration(
fun <T : IrElement> IrStatementsBuilder<T>.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
}
@@ -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")
}
}
@@ -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 <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R =
visitor.visitVariableDescriptor(this, data)
}
@@ -331,7 +331,7 @@ open class SerializerIrGenerator(
}
// returns null: Any? for boxed types and 0: <number type> for primitives
private fun IrBuilderWithScope.defaultValueAndType(prop: SerializableProperty): Pair<IrExpression, KotlinType> {
private fun IrBuilderWithScope.defaultValueAndType(prop: SerializableProperty): Pair<IrExpression, IrType> {
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
)
}