[JS IR BE] Fix name clashes between static and top-level functions

This commit is contained in:
Roman Artemev
2018-10-18 18:56:05 +03:00
committed by romanart
parent c496d8ed50
commit 41ccea6807
5 changed files with 26 additions and 8 deletions
@@ -33,6 +33,8 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.impl.IrDynamicTypeImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.getPropertyDeclaration
import org.jetbrains.kotlin.ir.util.kotlinPackageFqn
import org.jetbrains.kotlin.ir.util.referenceFunction
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
@@ -177,14 +179,14 @@ class JsIrBackendContext(
override val areEqual
get () = TODO("not implemented")
override val ThrowNullPointerException
get () = irBuiltIns.throwNpeSymbol
override val ThrowNullPointerException = getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))).singleOrNull()?.let {
symbolTable.referenceSimpleFunction(it) } ?: irBuiltIns.throwNpeSymbol
override val ThrowNoWhenBranchMatchedException
get () = irBuiltIns.noWhenBranchMatchedExceptionSymbol
override val ThrowTypeCastException
get () = irBuiltIns.throwCceSymbol
override val ThrowTypeCastException = getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))).singleOrNull()?.let {
symbolTable.referenceSimpleFunction(it) } ?: irBuiltIns.throwCceSymbol
override val ThrowUninitializedPropertyAccessException=
symbolTable.referenceSimpleFunction(getFunctions(FqName("kotlin.throwUninitializedPropertyAccessException")).single())
@@ -205,6 +207,9 @@ class JsIrBackendContext(
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
}
val throwISEymbol = getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))).singleOrNull()?.let {
symbolTable.referenceSimpleFunction(it) } ?: irBuiltIns.throwIseSymbol
val coroutineImplLabelProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("state")!! }
val coroutineImplResultSymbol by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("result")!! }
val coroutineImplExceptionProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("exception")!! }
@@ -60,6 +60,7 @@ class EnumClassTransformer(val context: JsIrBackendContext, private val irClass:
private val enumEntries = irClass.declarations.filterIsInstance<IrEnumEntry>()
private val loweredEnumConstructors = HashMap<IrConstructorSymbol, IrConstructor>()
private val enumName = irClass.name.identifier
private val throwISESymbol = context.throwISEymbol
fun transform(): List<IrDeclaration> {
// Add `name` and `ordinal` parameters to enum class constructors
@@ -140,7 +141,7 @@ class EnumClassTransformer(val context: JsIrBackendContext, private val irClass:
irBranch(
irEquals(irString(it.name.identifier), irGet(nameParameter)), irCall(getInstance)
)
} + irElseBranch(irCall(context.irBuiltIns.throwIseSymbol))
} + irElseBranch(irCall(throwISESymbol))
)
)
}
@@ -42,8 +42,8 @@ class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
private val calculator = JsIrArithBuilder(context)
//NOTE: Should we define JS-own functions similar to current implementation?
private val throwCCE = context.irBuiltIns.throwCceSymbol
private val throwNPE = context.irBuiltIns.throwNpeSymbol
private val throwCCE = context.ir.symbols.ThrowTypeCastException
private val throwNPE = context.ir.symbols.ThrowNullPointerException
private val eqeq = context.irBuiltIns.eqeqSymbol
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs
import org.jetbrains.kotlin.ir.backend.js.utils.JsGenerationContext
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.js.backend.ast.JsDeclarationScope
import org.jetbrains.kotlin.js.backend.ast.JsEmpty
import org.jetbrains.kotlin.js.backend.ast.JsStatement
import org.jetbrains.kotlin.js.backend.ast.JsVars
@@ -23,7 +24,15 @@ class IrDeclarationToJsTransformer : BaseIrElementToJsNodeTransformer<JsStatemen
}
override fun visitClass(declaration: IrClass, context: JsGenerationContext): JsStatement {
return JsClassGenerator(declaration, context).generate()
return JsClassGenerator(
declaration,
context.newDeclaration(
JsDeclarationScope(
context.currentScope,
"scope for class ${declaration.name.asString()}"
)
)
).generate()
}
override fun visitField(declaration: IrField, context: JsGenerationContext): JsStatement {
@@ -181,6 +181,9 @@ class SimpleNameGenerator : NameGenerator {
nameBuilder.append(getNameForDeclaration(declaration.parent as IrDeclaration, context))
nameBuilder.append('.')
}
if (declaration.dispatchReceiverParameter == null) {
nameDeclarator = context.staticContext.rootScope::declareFreshName
}
nameBuilder.append(declaration.name.asString())
// TODO should we skip type parameters and use upper bound of type parameter when print type of value parameters?