JVM_IR. Support compile time constants

This commit is contained in:
Georgy Bronnikov
2018-09-18 14:49:43 +03:00
parent 985934a40a
commit 055215c54f
39 changed files with 35 additions and 42 deletions
@@ -13,7 +13,9 @@ import org.jetbrains.kotlin.descriptors.PropertyGetterDescriptor
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
@@ -22,6 +24,7 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
class ConstAndJvmFieldPropertiesLowering(val context: JvmBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(this)
}
@@ -36,6 +39,12 @@ class ConstAndJvmFieldPropertiesLowering(val context: JvmBackendContext) : IrEle
}
override fun visitCall(expression: IrCall): IrExpression {
val irProperty = (expression.symbol.owner as? IrSimpleFunction)?.correspondingProperty ?: return super.visitCall(expression)
if (irProperty.isConst) {
(irProperty.backingField?.initializer?.expression as? IrConst<*>)?.let { return it }
}
val descriptor = expression.descriptor as? PropertyAccessorDescriptor ?: return super.visitCall(expression)
val property = descriptor.correspondingProperty
@@ -65,7 +74,7 @@ class ConstAndJvmFieldPropertiesLowering(val context: JvmBackendContext) : IrEle
)
}
private fun substituteGetter(descriptor: PropertyGetterDescriptor, expression: IrCall): IrGetFieldImpl {
private fun substituteGetter(descriptor: PropertyGetterDescriptor, expression: IrCall): IrExpression {
return IrGetFieldImpl(
expression.startOffset,
expression.endOffset,
@@ -79,14 +79,21 @@ class DeclarationStubGenerator(
throw AssertionError("Unexpected member descriptor: $descriptor")
}
internal fun generatePropertyStub(descriptor: PropertyDescriptor, bindingContext: BindingContext? = null): IrProperty =
internal fun generatePropertyStub(
descriptor: PropertyDescriptor,
bindingContext: BindingContext? = null
): IrProperty =
IrPropertyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor).also { irProperty ->
if (descriptor.hasBackingField(bindingContext)) {
irProperty.backingField = generateFieldStub(descriptor)
}
irProperty.getter = descriptor.getter?.let { generateFunctionStub(it) }
irProperty.setter = descriptor.setter?.let { generateFunctionStub(it) }
irProperty.getter = descriptor.getter?.let { generateFunctionStub(it, createPropertyIfNeeded = false) }?.apply {
correspondingProperty = irProperty
}
irProperty.setter = descriptor.setter?.let { generateFunctionStub(it, createPropertyIfNeeded = false) }?.apply {
correspondingProperty = irProperty
}
}
private fun generateFieldStub(descriptor: PropertyDescriptor): IrField {
@@ -117,12 +124,19 @@ class DeclarationStubGenerator(
}
}
fun generateFunctionStub(descriptor: FunctionDescriptor): IrSimpleFunction {
fun generateFunctionStub(descriptor: FunctionDescriptor, createPropertyIfNeeded: Boolean = true): IrSimpleFunction {
val referenced = symbolTable.referenceSimpleFunction(descriptor)
if (referenced.isBound) {
return referenced.owner
}
if (createPropertyIfNeeded && descriptor is PropertyGetterDescriptor) {
return generatePropertyStub(descriptor.correspondingProperty).getter!!
}
if (createPropertyIfNeeded && descriptor is PropertySetterDescriptor) {
return generatePropertyStub(descriptor.correspondingProperty).setter!!
}
val origin =
if (descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE)
IrDeclarationOrigin.FAKE_OVERRIDE