[JS IR BE] update current lowerings for IrScript

This commit is contained in:
Vitaliy.Tikhonov
2019-08-30 15:15:46 +03:00
committed by romanart
parent d79279d8a5
commit 240abdb750
14 changed files with 116 additions and 20 deletions
@@ -44,8 +44,10 @@ class JsIrBackendContext(
val symbolTable: SymbolTable,
irModuleFragment: IrModuleFragment,
val additionalExportedDeclarations: Set<FqName>,
override val configuration: CompilerConfiguration
override val configuration: CompilerConfiguration,
override val scriptMode: Boolean = false
) : CommonBackendContext {
override val transformedFunction = mutableMapOf<IrFunctionSymbol, IrSimpleFunctionSymbol>()
override val builtIns = module.builtIns
@@ -39,6 +39,10 @@ abstract class AbstractBlockDecomposerLowering(context: CommonBackendContext) :
override fun lower(irDeclarationContainer: IrDeclarationContainer) {
irDeclarationContainer.transformDeclarationsFlat { declaration ->
when (declaration) {
is IrScript -> {
lower(declaration)
listOf(declaration)
}
is IrFunction -> {
lower(declaration)
listOf(declaration)
@@ -49,6 +53,10 @@ abstract class AbstractBlockDecomposerLowering(context: CommonBackendContext) :
}
}
fun lower(irScript: IrScript) {
irScript.transform(decomposerTransformer, null)
}
fun lower(irFunction: IrFunction) {
(irFunction.body as? IrExpressionBody)?.apply {
irFunction.body = toBlockBody(irFunction)
@@ -94,7 +102,7 @@ class BlockDecomposerTransformer(
private val context: CommonBackendContext,
private val unreachableExpression: () -> IrExpression
) : IrElementTransformerVoid() {
private lateinit var function: IrFunction
private lateinit var function: IrDeclarationParent
private var tmpVarCounter: Int = 0
private val statementTransformer = StatementTransformer()
@@ -109,6 +117,30 @@ class BlockDecomposerTransformer(
private val booleanNotSymbol = context.irBuiltIns.booleanNotSymbol
override fun visitScript(declaration: IrScript): IrStatement {
function = declaration
with(declaration) {
val transformedDeclarations = declarations.map { it.transform(statementTransformer, null) as IrDeclaration }
declarations.clear()
declarations.addAll(transformedDeclarations)
val transformedStatements = mutableListOf<IrStatement>()
statements.forEach {
val transformer = if (it === statements.last()) expressionTransformer else statementTransformer
val s = it.transform(transformer, null)
if (s is IrComposite) {
transformedStatements.addAll(s.statements)
} else {
transformedStatements.add(s)
}
}
statements.clear()
statements.addAll(transformedStatements)
}
return declaration
}
override fun visitFunction(declaration: IrFunction): IrStatement {
function = declaration
tmpVarCounter = 0
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
@@ -27,7 +28,11 @@ class PrimitiveCompanionLowering(val context: JsIrBackendContext) : FileLowering
if (!irClass.isCompanion)
return null
val parent = irClass.parent as IrClass
//TODO: Figure out how to check for primitive companion in case similar to REPL in better way
val parent = irClass.parent as? IrClass
?: context.symbolTable.referenceClass(irClass.descriptor.containingDeclaration as ClassDescriptor).owner.also {
assert(context.scriptMode)
}
if (!parent.defaultType.isPrimitiveType() && !parent.defaultType.isString())
return null