[kotlin compiler][update][fixup] adoption SharedVariablesLowering to new Shared Variable Manager

This commit is contained in:
Vasily Levchenko
2018-05-08 16:47:10 +03:00
parent aa3cae7a4e
commit 2985cc0b62
@@ -17,18 +17,16 @@
package org.jetbrains.kotlin.backend.konan.lower
import org.jetbrains.kotlin.backend.common.*
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ValueDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.expressions.IrSetVariable
import org.jetbrains.kotlin.ir.expressions.IrValueAccessExpression
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.visitors.*
import java.util.*
@@ -39,7 +37,7 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
}
private inner class SharedVariablesTransformer(val irFunction: IrFunction) {
val sharedVariables = mutableSetOf<ValueDescriptor>()
val sharedVariables = mutableSetOf<IrVariable>()
fun lowerSharedVariables() {
collectSharedVariables()
@@ -50,8 +48,8 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
private fun collectSharedVariables() {
irFunction.acceptVoid(object : IrElementVisitorVoid {
val relevantVars = mutableSetOf<VariableDescriptor>()
val functionsVariables = mutableListOf<MutableSet<VariableDescriptor>>()
val relevantVars = mutableSetOf<IrVariable>()
val functionsVariables = mutableListOf<MutableSet<IrVariable>>()
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
@@ -66,39 +64,38 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
override fun visitVariable(declaration: IrVariable) {
declaration.acceptChildrenVoid(this)
val variableDescriptor = declaration.descriptor
if (variableDescriptor.isVar) {
relevantVars.add(variableDescriptor)
functionsVariables.peek()!!.add(variableDescriptor)
if (declaration.isVar) {
relevantVars.add(declaration)
functionsVariables.peek()!!.add(declaration)
}
}
override fun visitVariableAccess(expression: IrValueAccessExpression) {
expression.acceptChildrenVoid(this)
val descriptor = expression.descriptor
val value = expression.symbol.owner
//if (descriptor in relevantVars && descriptor.containingDeclaration != currentDeclaration) {
if (descriptor in relevantVars && !functionsVariables.peek()!!.contains(descriptor)) {
sharedVariables.add(descriptor)
// TODO: fix lowerings to match check `(value as IrVariable).parent != currentDeclaration`
if (value in relevantVars && !functionsVariables.peek()!!.contains(value)) {
sharedVariables.add(value as IrVariable)
}
}
})
}
private fun rewriteSharedVariables() {
val transformedDescriptors = HashMap<ValueDescriptor, VariableDescriptor>()
val transformedVariableSymbols = HashMap<IrValueSymbol, IrVariableSymbol>()
irFunction.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitVariable(declaration: IrVariable): IrStatement {
declaration.transformChildrenVoid(this)
val oldDescriptor = declaration.descriptor
if (oldDescriptor !in sharedVariables) return declaration
if (declaration !in sharedVariables) return declaration
val newDescriptor = context.sharedVariablesManager.createSharedVariableDescriptor(oldDescriptor)
transformedDescriptors[oldDescriptor] = newDescriptor
val newDeclaration = context.sharedVariablesManager.declareSharedVariable(declaration)
transformedVariableSymbols[declaration.symbol] = newDeclaration.symbol
return context.sharedVariablesManager.defineSharedValue(newDescriptor, declaration)
return context.sharedVariablesManager.defineSharedValue(declaration, newDeclaration)
}
})
@@ -106,23 +103,23 @@ class SharedVariablesLowering(val context: BackendContext) : FunctionLoweringPas
override fun visitGetValue(expression: IrGetValue): IrExpression {
expression.transformChildrenVoid(this)
val newDescriptor = getTransformedDescriptor(expression.descriptor) ?: return expression
val newSymbol = getTransformedSymbol(expression.symbol) ?: return expression
return context.sharedVariablesManager.getSharedValue(newDescriptor, expression)
return context.sharedVariablesManager.getSharedValue(newSymbol, expression)
}
override fun visitSetVariable(expression: IrSetVariable): IrExpression {
expression.transformChildrenVoid(this)
val newDescriptor = getTransformedDescriptor(expression.descriptor) ?: return expression
val newDescriptor = getTransformedSymbol(expression.symbol) ?: return expression
return context.sharedVariablesManager.setSharedValue(newDescriptor, expression)
}
private fun getTransformedDescriptor(oldDescriptor: ValueDescriptor): VariableDescriptor? =
transformedDescriptors.getOrElse(oldDescriptor) {
assert(oldDescriptor !in sharedVariables) {
"Shared variable is not transformed: $oldDescriptor"
private fun getTransformedSymbol(oldSymbol: IrValueSymbol): IrVariableSymbol? =
transformedVariableSymbols.getOrElse(oldSymbol) {
assert(oldSymbol.owner !in sharedVariables) {
"Shared variable is not transformed: $oldSymbol"
}
null
}