From e9cdad1143d088ee69324e23503323814231fb11 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Thu, 21 Feb 2019 19:32:56 +0300 Subject: [PATCH] [JS IR BE] Remove support for dynamic symbols and descriptors Dynamic expressions in IR are now represented as IrDynamicOperatorExpression We don't have dynamic functions, dynamic dispatch receivers anymore --- .../backend/common/CheckIrElementVisitor.kt | 10 +++ .../lower/InlineClassDeclarationLowering.kt | 4 +- .../backend/js/lower/calls/CallsLowering.kt | 1 - .../js/lower/calls/DynamicCallsTransformer.kt | 90 ------------------- .../backend/js/utils/SimpleNameGenerator.kt | 28 +----- .../org/jetbrains/kotlin/ir/util/IrUtils.kt | 2 - 6 files changed, 12 insertions(+), 123 deletions(-) delete mode 100644 compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/DynamicCallsTransformer.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CheckIrElementVisitor.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CheckIrElementVisitor.kt index d9684c44fbe..fbe0ffb6ef4 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CheckIrElementVisitor.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/CheckIrElementVisitor.kt @@ -126,6 +126,12 @@ class CheckIrElementVisitor( override fun visitCall(expression: IrCall) { super.visitCall(expression) + val function = expression.symbol.owner + + if (function.dispatchReceiverParameter?.type is IrDynamicType) { + reportError(expression, "Dispatch receivers with 'dynamic' type are not allowed") + } + val returnType = expression.descriptor.returnType if (returnType == null) { reportError(expression, "${expression.descriptor} return type is null") @@ -232,6 +238,10 @@ class CheckIrElementVisitor( override fun visitFunction(declaration: IrFunction) { super.visitFunction(declaration) + if (declaration.dispatchReceiverParameter?.type is IrDynamicType) { + reportError(declaration, "Dispatch receivers with 'dynamic' type are not allowed") + } + for ((i, p) in declaration.valueParameters.withIndex()) { if (p.index != i) { reportError(declaration, "Inconsistent index of value parameter ${p.index} != $i") diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineClassDeclarationLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineClassDeclarationLowering.kt index 5298ae27503..6dd553621a3 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineClassDeclarationLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/InlineClassDeclarationLowering.kt @@ -188,9 +188,7 @@ class InlineClassLowering(val context: BackendContext) { override fun visitCall(call: IrCall): IrExpression { call.transformChildrenVoid(this) val function = call.symbol.owner - if ( - function.isDynamic() || - function.parent !is IrClass || + if (function.parent !is IrClass || function.isStaticMethodOfClass || !function.parentAsClass.isInline || (function is IrSimpleFunction && !function.isReal) || diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt index 15c79599051..4b132bcf132 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/CallsLowering.kt @@ -20,7 +20,6 @@ class CallsLowering(val context: JsIrBackendContext) : FileLoweringPass { private val transformers = listOf( NumberOperatorCallsTransformer(context), NumberConversionCallsTransformer(context), - DynamicCallsTransformer(context), EqualityAndComparisonCallsTransformer(context), PrimitiveContainerMemberCallTransformer(context), MethodsOfAnyCallsTransformer(context), diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/DynamicCallsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/DynamicCallsTransformer.kt deleted file mode 100644 index 8dd0bbc8f8d..00000000000 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/DynamicCallsTransformer.kt +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.ir.backend.js.lower.calls - -import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor -import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext -import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder -import org.jetbrains.kotlin.ir.util.irCall -import org.jetbrains.kotlin.ir.expressions.IrCall -import org.jetbrains.kotlin.ir.expressions.IrExpression -import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin.* -import org.jetbrains.kotlin.ir.util.isDynamic -import org.jetbrains.kotlin.ir.util.isEffectivelyExternal - -class DynamicCallsTransformer(private val context: JsIrBackendContext) : CallsTransformer { - - private val originToIrFunction = - context.intrinsics.run { - mapOf( - EXCL to jsNot, - LT to jsLt, - GT to jsGt, - LTEQ to jsLtEq, - GTEQ to jsGtEq, - EQEQ to jsEqeq, - EQEQEQ to jsEqeqeq, - EXCLEQ to jsNotEq, - EXCLEQEQ to jsNotEqeq, - ANDAND to jsAnd, - OROR to jsOr, - UMINUS to jsUnaryMinus, - UPLUS to jsUnaryPlus, - PLUS to jsPlus, - MINUS to jsMinus, - MUL to jsMult, - DIV to jsDiv, - PERC to jsMod, - PLUSEQ to jsPlusAssign, - MINUSEQ to jsMinusAssign, - MULTEQ to jsMultAssign, - DIVEQ to jsDivAssign, - PERCEQ to jsModAssign, - PREFIX_INCR to jsPrefixInc, - PREFIX_DECR to jsPrefixDec, - POSTFIX_INCR to jsPostfixInc, - POSTFIX_DECR to jsPostfixDec, - GET_ARRAY_ELEMENT to jsArrayGet, - // TODO add a special statement origin, e.g. SET_ARRAY_ELEMENT - EQ to jsArraySet - ) - } - - override fun transformCall(call: IrCall): IrExpression { - val symbol = call.symbol - val function = call.symbol.owner - - if (function.isDynamic()) { - when (call.origin) { - GET_PROPERTY -> { - val fieldSymbol = context.symbolTable.lazyWrapper.referenceField( - (symbol.descriptor as PropertyAccessorDescriptor).correspondingProperty - ) - return JsIrBuilder.buildGetField(fieldSymbol, call.dispatchReceiver, type = call.type) - } - - // assignment to a property - EQ -> { - if (symbol.descriptor is PropertyAccessorDescriptor) { - val fieldSymbol = context.symbolTable.lazyWrapper.referenceField( - (symbol.descriptor as PropertyAccessorDescriptor).correspondingProperty - ) - return call.run { - JsIrBuilder.buildSetField(fieldSymbol, dispatchReceiver, getValueArgument(0)!!, type) - } - } - } - } - } - - if (function.isDynamic()) { - originToIrFunction[call.origin]?.let { - return irCall(call, it.symbol, dispatchReceiverAsFirstArgument = true) - } - } - return call - } -} diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt index b0ebac44787..df586de25c8 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/SimpleNameGenerator.kt @@ -13,15 +13,12 @@ import org.jetbrains.kotlin.ir.expressions.IrLoop import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classifierOrFail -import org.jetbrains.kotlin.ir.util.isDynamic import org.jetbrains.kotlin.ir.util.isEffectivelyExternal import org.jetbrains.kotlin.ir.util.isInlined import org.jetbrains.kotlin.ir.util.parentAsClass import org.jetbrains.kotlin.js.backend.ast.JsName import org.jetbrains.kotlin.js.naming.isES5IdentifierPart import org.jetbrains.kotlin.js.naming.isES5IdentifierStart -import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic -import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty @@ -32,8 +29,7 @@ class SimpleNameGenerator : NameGenerator { private val loopCache = mutableMapOf() override fun getNameForSymbol(symbol: IrSymbol, context: JsGenerationContext): JsName = - if (symbol.isBound) getNameForDeclaration(symbol.owner as IrDeclarationWithName, context) else - declareDynamic(symbol.descriptor, context) + getNameForDeclaration(symbol.owner as IrDeclarationWithName, context) override fun getNameForLoop(loop: IrLoop, context: JsGenerationContext): JsName? = loop.label?.let { loopCache.getOrPut(loop) { context.currentScope.declareFreshName(sanitizeName(loop.label!!)) } @@ -42,24 +38,6 @@ class SimpleNameGenerator : NameGenerator { override fun getNameForType(type: IrType, context: JsGenerationContext) = getNameForDeclaration(type.classifierOrFail.owner as IrDeclarationWithName, context) - @Deprecated("Descriptors-based code is deprecated") - private fun declareDynamic(descriptor: DeclarationDescriptor, context: JsGenerationContext): JsName { - if (descriptor.isDynamic()) { - return context.currentScope.declareName(descriptor.name.asString()) - } - - if (descriptor is MemberDescriptor && descriptor.isEffectivelyExternal()) { - val descriptorForName = when (descriptor) { - is ConstructorDescriptor -> descriptor.constructedClass - is PropertyAccessorDescriptor -> descriptor.correspondingProperty - else -> descriptor - } - return context.currentScope.declareName(descriptorForName.name.asString()) - } - - throw IllegalStateException("Unbound non-dynamic symbol") - } - private val RESERVED_IDENTIFIERS = setOf( // keywords "await", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for", "function", "if", @@ -96,10 +74,6 @@ class SimpleNameGenerator : NameGenerator { var nameDeclarator: (String) -> JsName = context.currentScope::declareName val nameBuilder = StringBuilder() - if (declaration.isDynamic()) { - return@getOrPut nameDeclarator(declaration.descriptor.name.asString()) - } - val declarationName = declaration.getJsNameOrKotlinName().asString() if (declaration is IrSimpleFunction && declaration.origin == JsLoweredDeclarationOrigin.BRIDGE_TO_EXTERNAL_FUNCTION) { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index 01afde35c4f..02f5480dcfe 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -424,8 +424,6 @@ fun IrDeclaration.isEffectivelyExternal(): Boolean { } } -fun IrDeclaration.isDynamic() = this is IrFunction && dispatchReceiverParameter?.type is IrDynamicType - inline fun IrDeclarationContainer.findDeclaration(predicate: (T) -> Boolean): T? = declarations.find { it is T && predicate(it) } as? T