Minor. Reformat
This commit is contained in:
+21
-20
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.codegen.FunctionCodegen
|
|||||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||||
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrConstructor
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||||
import org.jetbrains.kotlin.ir.util.dump
|
import org.jetbrains.kotlin.ir.util.dump
|
||||||
import org.jetbrains.kotlin.psi.KtParameter
|
import org.jetbrains.kotlin.psi.KtParameter
|
||||||
@@ -46,8 +47,7 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
|
|||||||
fun generate() {
|
fun generate() {
|
||||||
try {
|
try {
|
||||||
doGenerate()
|
doGenerate()
|
||||||
}
|
} catch (e: Throwable) {
|
||||||
catch (e: Throwable) {
|
|
||||||
throw RuntimeException("${e.message} while generating code for:\n${irFunction.dump()}", e)
|
throw RuntimeException("${e.message} while generating code for:\n${irFunction.dump()}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
|
|||||||
|
|
||||||
private fun calculateMethodFlags(isStatic: Boolean): Int {
|
private fun calculateMethodFlags(isStatic: Boolean): Int {
|
||||||
var flags = AsmUtil.getMethodAsmFlags(descriptor, OwnerKind.IMPLEMENTATION, state).or(if (isStatic) Opcodes.ACC_STATIC else 0).xor(
|
var flags = AsmUtil.getMethodAsmFlags(descriptor, OwnerKind.IMPLEMENTATION, state).or(if (isStatic) Opcodes.ACC_STATIC else 0).xor(
|
||||||
if (DescriptorUtils.isAnnotationClass(descriptor.containingDeclaration)) Opcodes.ACC_FINAL else 0/*TODO*/
|
if (DescriptorUtils.isAnnotationClass(descriptor.containingDeclaration)) Opcodes.ACC_FINAL else 0/*TODO*/
|
||||||
).or(if (descriptor is JvmDescriptorWithExtraFlags) descriptor.extraFlags else 0)
|
).or(if (descriptor is JvmDescriptorWithExtraFlags) descriptor.extraFlags else 0)
|
||||||
|
|
||||||
if (irFunction.origin == DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER) {
|
if (irFunction.origin == DECLARATION_ORIGIN_FUNCTION_FOR_DEFAULT_PARAMETER) {
|
||||||
@@ -91,10 +91,12 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
|
|||||||
}
|
}
|
||||||
|
|
||||||
open protected fun createMethod(flags: Int, signature: JvmMethodGenericSignature): MethodVisitor {
|
open protected fun createMethod(flags: Int, signature: JvmMethodGenericSignature): MethodVisitor {
|
||||||
return classCodegen.visitor.newMethod(irFunction.OtherOrigin,
|
return classCodegen.visitor.newMethod(
|
||||||
flags,
|
irFunction.OtherOrigin,
|
||||||
signature.asmMethod.name, signature.asmMethod.descriptor,
|
flags,
|
||||||
signature.genericsSignature, null/*TODO support exception*/)
|
signature.asmMethod.name, signature.asmMethod.descriptor,
|
||||||
|
signature.genericsSignature, null/*TODO support exception*/
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateAnnotationDefaultValueIfNeeded(methodVisitor: MethodVisitor) {
|
private fun generateAnnotationDefaultValueIfNeeded(methodVisitor: MethodVisitor) {
|
||||||
@@ -104,7 +106,8 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
|
|||||||
(source.getPsi() as? KtParameter)?.defaultValue?.apply {
|
(source.getPsi() as? KtParameter)?.defaultValue?.apply {
|
||||||
val defaultValue = this
|
val defaultValue = this
|
||||||
val constant = org.jetbrains.kotlin.codegen.ExpressionCodegen.getCompileTimeConstant(
|
val constant = org.jetbrains.kotlin.codegen.ExpressionCodegen.getCompileTimeConstant(
|
||||||
defaultValue, state.bindingContext, true, state.shouldInlineConstVals)
|
defaultValue, state.bindingContext, true, state.shouldInlineConstVals
|
||||||
|
)
|
||||||
assert(!state.classBuilderMode.generateBodies || constant != null) { "Default value for annotation parameter should be compile time value: " + defaultValue.text }
|
assert(!state.classBuilderMode.generateBodies || constant != null) { "Default value for annotation parameter should be compile time value: " + defaultValue.text }
|
||||||
if (constant != null) {
|
if (constant != null) {
|
||||||
val annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(methodVisitor, classCodegen, state.typeMapper)
|
val annotationCodegen = AnnotationCodegen.forAnnotationDefaultValue(methodVisitor, classCodegen, state.typeMapper)
|
||||||
@@ -116,18 +119,18 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun createFrameMapWithReceivers(
|
fun createFrameMapWithReceivers(
|
||||||
state: GenerationState,
|
state: GenerationState,
|
||||||
function: FunctionDescriptor,
|
function: FunctionDescriptor,
|
||||||
signature: JvmMethodSignature,
|
signature: JvmMethodSignature,
|
||||||
isStatic: Boolean
|
isStatic: Boolean
|
||||||
): FrameMap {
|
): FrameMap {
|
||||||
val frameMap = FrameMap()
|
val frameMap = FrameMap()
|
||||||
if (!isStatic) {
|
if (!isStatic) {
|
||||||
val descriptorForThis =
|
val descriptorForThis =
|
||||||
if (function is ClassConstructorDescriptor)
|
if (function is ClassConstructorDescriptor)
|
||||||
function.containingDeclaration.thisAsReceiverParameter
|
function.containingDeclaration.thisAsReceiverParameter
|
||||||
else
|
else
|
||||||
function.dispatchReceiverParameter
|
function.dispatchReceiverParameter
|
||||||
|
|
||||||
frameMap.enter(descriptorForThis, AsmTypes.OBJECT_TYPE)
|
frameMap.enter(descriptorForThis, AsmTypes.OBJECT_TYPE)
|
||||||
}
|
}
|
||||||
@@ -137,12 +140,10 @@ fun createFrameMapWithReceivers(
|
|||||||
val receiverParameter = function.extensionReceiverParameter
|
val receiverParameter = function.extensionReceiverParameter
|
||||||
if (receiverParameter != null) {
|
if (receiverParameter != null) {
|
||||||
frameMap.enter(receiverParameter, state.typeMapper.mapType(receiverParameter))
|
frameMap.enter(receiverParameter, state.typeMapper.mapType(receiverParameter))
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
frameMap.enterTemp(parameter.asmType)
|
frameMap.enterTemp(parameter.asmType)
|
||||||
}
|
}
|
||||||
}
|
} else if (parameter.kind != JvmMethodParameterKind.VALUE) {
|
||||||
else if (parameter.kind != JvmMethodParameterKind.VALUE) {
|
|
||||||
frameMap.enterTemp(parameter.asmType)
|
frameMap.enterTemp(parameter.asmType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+77
-56
@@ -89,8 +89,8 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
val functions = irClass.declarations.filterIsInstance<IrFunction>().filterNot {
|
val functions = irClass.declarations.filterIsInstance<IrFunction>().filterNot {
|
||||||
val descriptor = it.descriptor
|
val descriptor = it.descriptor
|
||||||
descriptor is ConstructorDescriptor ||
|
descriptor is ConstructorDescriptor ||
|
||||||
DescriptorUtils.isStaticDeclaration(descriptor) ||
|
DescriptorUtils.isStaticDeclaration(descriptor) ||
|
||||||
!descriptor.kind.isReal
|
!descriptor.kind.isReal
|
||||||
}
|
}
|
||||||
|
|
||||||
functions.forEach {
|
functions.forEach {
|
||||||
@@ -105,8 +105,7 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
if (!memberDescriptor.kind.isReal && findInterfaceImplementation(memberDescriptor) == null) {
|
if (!memberDescriptor.kind.isReal && findInterfaceImplementation(memberDescriptor) == null) {
|
||||||
if (memberDescriptor is FunctionDescriptor) {
|
if (memberDescriptor is FunctionDescriptor) {
|
||||||
generateBridges(memberDescriptor, irClass)
|
generateBridges(memberDescriptor, irClass)
|
||||||
}
|
} else if (memberDescriptor is PropertyDescriptor) {
|
||||||
else if (memberDescriptor is PropertyDescriptor) {
|
|
||||||
val getter = memberDescriptor.getter
|
val getter = memberDescriptor.getter
|
||||||
if (getter != null) {
|
if (getter != null) {
|
||||||
generateBridges(getter, irClass)
|
generateBridges(getter, irClass)
|
||||||
@@ -133,44 +132,47 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
val bridgesToGenerate: Set<Bridge<SignatureAndDescriptor>>
|
val bridgesToGenerate: Set<Bridge<SignatureAndDescriptor>>
|
||||||
if (!isSpecial) {
|
if (!isSpecial) {
|
||||||
bridgesToGenerate = generateBridgesForFunctionDescriptor(
|
bridgesToGenerate = generateBridgesForFunctionDescriptor(
|
||||||
descriptor,
|
descriptor,
|
||||||
getSignatureMapper(typeMapper),
|
getSignatureMapper(typeMapper),
|
||||||
DECLARATION_AND_DEFINITION_CHECKER
|
DECLARATION_AND_DEFINITION_CHECKER
|
||||||
)
|
)
|
||||||
if (!bridgesToGenerate.isEmpty()) {
|
if (!bridgesToGenerate.isEmpty()) {
|
||||||
val origin = if (descriptor.kind == DECLARATION) getSourceFromDescriptor(descriptor) else null
|
val origin = if (descriptor.kind == DECLARATION) getSourceFromDescriptor(descriptor) else null
|
||||||
val isSpecialBridge = BuiltinMethodsWithSpecialGenericSignature.getOverriddenBuiltinFunctionWithErasedValueParametersInJava(descriptor) != null
|
val isSpecialBridge =
|
||||||
|
BuiltinMethodsWithSpecialGenericSignature.getOverriddenBuiltinFunctionWithErasedValueParametersInJava(descriptor) != null
|
||||||
|
|
||||||
for (bridge in bridgesToGenerate) {
|
for (bridge in bridgesToGenerate) {
|
||||||
irClass.declarations.add(createBridge(origin, descriptor, bridge.from, bridge.to, isSpecialBridge, false))
|
irClass.declarations.add(createBridge(origin, descriptor, bridge.from, bridge.to, isSpecialBridge, false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
val specials = BuiltinSpecialBridgesUtil.generateBridgesForBuiltinSpecial(
|
val specials = BuiltinSpecialBridgesUtil.generateBridgesForBuiltinSpecial(
|
||||||
descriptor,
|
descriptor,
|
||||||
getSignatureMapper(typeMapper),
|
getSignatureMapper(typeMapper),
|
||||||
DECLARATION_AND_DEFINITION_CHECKER
|
DECLARATION_AND_DEFINITION_CHECKER
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!specials.isEmpty()) {
|
if (!specials.isEmpty()) {
|
||||||
val origin = if (descriptor.kind == DECLARATION) getSourceFromDescriptor(descriptor) else null
|
val origin = if (descriptor.kind == DECLARATION) getSourceFromDescriptor(descriptor) else null
|
||||||
for (bridge in specials) {
|
for (bridge in specials) {
|
||||||
irClass.declarations.add(createBridge(
|
irClass.declarations.add(
|
||||||
|
createBridge(
|
||||||
origin, descriptor, bridge.from, bridge.to,
|
origin, descriptor, bridge.from, bridge.to,
|
||||||
bridge.isSpecial, bridge.isDelegateToSuper))
|
bridge.isSpecial, bridge.isDelegateToSuper
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!descriptor.kind.isReal && isAbstractMethod(descriptor, OwnerKind.IMPLEMENTATION)) {
|
if (!descriptor.kind.isReal && isAbstractMethod(descriptor, OwnerKind.IMPLEMENTATION)) {
|
||||||
descriptor.getOverriddenBuiltinReflectingJvmDescriptor<CallableMemberDescriptor>() ?:
|
descriptor.getOverriddenBuiltinReflectingJvmDescriptor<CallableMemberDescriptor>()
|
||||||
error("Expect to find overridden descriptors for $descriptor")
|
?: error("Expect to find overridden descriptors for $descriptor")
|
||||||
|
|
||||||
if (!isThereOverriddenInKotlinClass(descriptor)) {
|
if (!isThereOverriddenInKotlinClass(descriptor)) {
|
||||||
val flags = Opcodes.ACC_ABSTRACT or getVisibilityAccessFlag(descriptor)
|
val flags = Opcodes.ACC_ABSTRACT or getVisibilityAccessFlag(descriptor)
|
||||||
val bridgeDescriptor = JvmFunctionDescriptorImpl(
|
val bridgeDescriptor = JvmFunctionDescriptorImpl(
|
||||||
descriptor.containingDeclaration as ClassDescriptor, null, Annotations.EMPTY,
|
descriptor.containingDeclaration as ClassDescriptor, null, Annotations.EMPTY,
|
||||||
descriptor.name, CallableMemberDescriptor.Kind.SYNTHESIZED, descriptor.source, flags
|
descriptor.name, CallableMemberDescriptor.Kind.SYNTHESIZED, descriptor.source, flags
|
||||||
)
|
)
|
||||||
val irFunction = IrFunctionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.DEFINED, bridgeDescriptor)
|
val irFunction = IrFunctionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.DEFINED, bridgeDescriptor)
|
||||||
irClass.declarations.add(irFunction)
|
irClass.declarations.add(irFunction)
|
||||||
@@ -180,26 +182,27 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createBridge(
|
private fun createBridge(
|
||||||
origin: PsiElement?,
|
origin: PsiElement?,
|
||||||
descriptor: FunctionDescriptor,
|
descriptor: FunctionDescriptor,
|
||||||
bridge: SignatureAndDescriptor,
|
bridge: SignatureAndDescriptor,
|
||||||
delegateTo: SignatureAndDescriptor,
|
delegateTo: SignatureAndDescriptor,
|
||||||
isSpecialBridge: Boolean,
|
isSpecialBridge: Boolean,
|
||||||
isStubDeclarationWithDelegationToSuper: Boolean
|
isStubDeclarationWithDelegationToSuper: Boolean
|
||||||
): IrFunction {
|
): IrFunction {
|
||||||
val isSpecialOrDelegationToSuper = isSpecialBridge || isStubDeclarationWithDelegationToSuper
|
val isSpecialOrDelegationToSuper = isSpecialBridge || isStubDeclarationWithDelegationToSuper
|
||||||
val flags = ACC_PUBLIC or ACC_BRIDGE or (if (!isSpecialOrDelegationToSuper) ACC_SYNTHETIC else 0) or if (isSpecialBridge) ACC_FINAL else 0 // TODO.
|
val flags =
|
||||||
|
ACC_PUBLIC or ACC_BRIDGE or (if (!isSpecialOrDelegationToSuper) ACC_SYNTHETIC else 0) or if (isSpecialBridge) ACC_FINAL else 0 // TODO.
|
||||||
val containingClass = descriptor.containingDeclaration as ClassDescriptor
|
val containingClass = descriptor.containingDeclaration as ClassDescriptor
|
||||||
//here some 'isSpecialBridge' magic
|
//here some 'isSpecialBridge' magic
|
||||||
val bridgeDescriptor = JvmFunctionDescriptorImpl(
|
val bridgeDescriptor = JvmFunctionDescriptorImpl(
|
||||||
containingClass, null, Annotations.EMPTY, Name.identifier(bridge.method.name),
|
containingClass, null, Annotations.EMPTY, Name.identifier(bridge.method.name),
|
||||||
CallableMemberDescriptor.Kind.SYNTHESIZED, descriptor.source, flags
|
CallableMemberDescriptor.Kind.SYNTHESIZED, descriptor.source, flags
|
||||||
)
|
)
|
||||||
|
|
||||||
bridgeDescriptor.initialize(
|
bridgeDescriptor.initialize(
|
||||||
null, containingClass.thisAsReceiverParameter, emptyList(),
|
null, containingClass.thisAsReceiverParameter, emptyList(),
|
||||||
bridge.descriptor.valueParameters.map { it.copy(bridgeDescriptor, it.name, it.index) },
|
bridge.descriptor.valueParameters.map { it.copy(bridgeDescriptor, it.name, it.index) },
|
||||||
bridge.descriptor.returnType, Modality.OPEN, descriptor.visibility
|
bridge.descriptor.returnType, Modality.OPEN, descriptor.visibility
|
||||||
)
|
)
|
||||||
|
|
||||||
val irFunction = IrFunctionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.DEFINED, bridgeDescriptor)
|
val irFunction = IrFunctionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrDeclarationOrigin.DEFINED, bridgeDescriptor)
|
||||||
@@ -212,12 +215,27 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val implementation = if (isSpecialBridge) delegateTo.descriptor.copyAsDeclaration() else delegateTo.descriptor
|
val implementation = if (isSpecialBridge) delegateTo.descriptor.copyAsDeclaration() else delegateTo.descriptor
|
||||||
val call = IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
val call = IrCallImpl(
|
||||||
implementation,
|
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||||
null, JvmLoweredStatementOrigin.BRIDGE_DELEGATION, null)
|
implementation,
|
||||||
call.dispatchReceiver = IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, containingClass.thisAsReceiverParameter, JvmLoweredStatementOrigin.BRIDGE_DELEGATION)
|
null, JvmLoweredStatementOrigin.BRIDGE_DELEGATION, null
|
||||||
|
)
|
||||||
|
call.dispatchReceiver = IrGetValueImpl(
|
||||||
|
UNDEFINED_OFFSET,
|
||||||
|
UNDEFINED_OFFSET,
|
||||||
|
containingClass.thisAsReceiverParameter,
|
||||||
|
JvmLoweredStatementOrigin.BRIDGE_DELEGATION
|
||||||
|
)
|
||||||
bridgeDescriptor.valueParameters.mapIndexed { i, valueParameterDescriptor ->
|
bridgeDescriptor.valueParameters.mapIndexed { i, valueParameterDescriptor ->
|
||||||
call.putValueArgument(i, IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valueParameterDescriptor, JvmLoweredStatementOrigin.BRIDGE_DELEGATION))
|
call.putValueArgument(
|
||||||
|
i,
|
||||||
|
IrGetValueImpl(
|
||||||
|
UNDEFINED_OFFSET,
|
||||||
|
UNDEFINED_OFFSET,
|
||||||
|
valueParameterDescriptor,
|
||||||
|
JvmLoweredStatementOrigin.BRIDGE_DELEGATION
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
+IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, bridgeDescriptor, call)
|
+IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, bridgeDescriptor, call)
|
||||||
}.apply {
|
}.apply {
|
||||||
@@ -228,25 +246,26 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun IrBlockBodyBuilder.generateTypeCheckBarrierIfNeeded(
|
private fun IrBlockBodyBuilder.generateTypeCheckBarrierIfNeeded(
|
||||||
overrideDescriptor: FunctionDescriptor,
|
overrideDescriptor: FunctionDescriptor,
|
||||||
bridgeDescriptor: FunctionDescriptor,
|
bridgeDescriptor: FunctionDescriptor,
|
||||||
delegateParameterTypes: Array<Type>?
|
delegateParameterTypes: Array<Type>?
|
||||||
) {
|
) {
|
||||||
val typeSafeBarrierDescription = BuiltinMethodsWithSpecialGenericSignature.getDefaultValueForOverriddenBuiltinFunction(overrideDescriptor) ?: return
|
val typeSafeBarrierDescription =
|
||||||
|
BuiltinMethodsWithSpecialGenericSignature.getDefaultValueForOverriddenBuiltinFunction(overrideDescriptor) ?: return
|
||||||
|
|
||||||
BuiltinMethodsWithSpecialGenericSignature.getOverriddenBuiltinFunctionWithErasedValueParametersInJava(overrideDescriptor) ?:
|
BuiltinMethodsWithSpecialGenericSignature.getOverriddenBuiltinFunctionWithErasedValueParametersInJava(overrideDescriptor)
|
||||||
error("Overridden built-in method should not be null for " + overrideDescriptor)
|
?: error("Overridden built-in method should not be null for " + overrideDescriptor)
|
||||||
|
|
||||||
val conditions = bridgeDescriptor.valueParameters.withIndex().filter { (i, parameterDescriptor) ->
|
val conditions = bridgeDescriptor.valueParameters.withIndex().filter { (i, parameterDescriptor) ->
|
||||||
typeSafeBarrierDescription.checkParameter(i) ||
|
typeSafeBarrierDescription.checkParameter(i) ||
|
||||||
!(delegateParameterTypes == null || OBJECT_TYPE == delegateParameterTypes[i]) ||
|
!(delegateParameterTypes == null || OBJECT_TYPE == delegateParameterTypes[i]) ||
|
||||||
!TypeUtils.isNullableType(parameterDescriptor.type)
|
!TypeUtils.isNullableType(parameterDescriptor.type)
|
||||||
}.map { (i, parameterDescriptor) ->
|
}.map { (i, parameterDescriptor) ->
|
||||||
val checkValue = IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, parameterDescriptor, JvmLoweredStatementOrigin.BRIDGE_DELEGATION)
|
val checkValue =
|
||||||
|
IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, parameterDescriptor, JvmLoweredStatementOrigin.BRIDGE_DELEGATION)
|
||||||
if (delegateParameterTypes == null || OBJECT_TYPE == delegateParameterTypes[i]) {
|
if (delegateParameterTypes == null || OBJECT_TYPE == delegateParameterTypes[i]) {
|
||||||
irNotEquals(checkValue, irNull())
|
irNotEquals(checkValue, irNull())
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
irIs(checkValue, overrideDescriptor.valueParameters[i].type)
|
irIs(checkValue, overrideDescriptor.valueParameters[i].type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,13 +277,14 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
|
|
||||||
+irIfThen(irNot(condition), irBlock {
|
+irIfThen(irNot(condition), irBlock {
|
||||||
+irReturn(
|
+irReturn(
|
||||||
when (typeSafeBarrierDescription) {
|
when (typeSafeBarrierDescription) {
|
||||||
MAP_GET_OR_DEFAULT -> irGet(IrVariableSymbolImpl(bridgeDescriptor.valueParameters[1]))
|
MAP_GET_OR_DEFAULT -> irGet(IrVariableSymbolImpl(bridgeDescriptor.valueParameters[1]))
|
||||||
BuiltinMethodsWithSpecialGenericSignature.TypeSafeBarrierDescription.NULL -> irNull()
|
BuiltinMethodsWithSpecialGenericSignature.TypeSafeBarrierDescription.NULL -> irNull()
|
||||||
INDEX -> IrConstImpl.int(
|
INDEX -> IrConstImpl.int(
|
||||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.builtIns.intType, typeSafeBarrierDescription.defaultValue as Int)
|
UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.builtIns.intType, typeSafeBarrierDescription.defaultValue as Int
|
||||||
FALSE -> irFalse()
|
)
|
||||||
}
|
FALSE -> irFalse()
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -276,14 +296,15 @@ class BridgeLowering(val context: JvmBackendContext) : ClassLoweringPass {
|
|||||||
companion object {
|
companion object {
|
||||||
fun getSignatureMapper(typeMapper: KotlinTypeMapper): Function1<FunctionDescriptor, SignatureAndDescriptor> {
|
fun getSignatureMapper(typeMapper: KotlinTypeMapper): Function1<FunctionDescriptor, SignatureAndDescriptor> {
|
||||||
return fun(descriptor: FunctionDescriptor) =
|
return fun(descriptor: FunctionDescriptor) =
|
||||||
SignatureAndDescriptor(typeMapper.mapAsmMethod(descriptor), descriptor)
|
SignatureAndDescriptor(typeMapper.mapAsmMethod(descriptor), descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun FunctionDescriptor.copyAsDeclaration(): FunctionDescriptor {
|
fun FunctionDescriptor.copyAsDeclaration(): FunctionDescriptor {
|
||||||
val isGetter = this is PropertyGetterDescriptor
|
val isGetter = this is PropertyGetterDescriptor
|
||||||
val isAccessor = this is PropertyAccessorDescriptor
|
val isAccessor = this is PropertyAccessorDescriptor
|
||||||
val directMember = getDirectMember(this)
|
val directMember = getDirectMember(this)
|
||||||
val copy = directMember.copy(directMember.containingDeclaration, directMember.modality, directMember.visibility, DECLARATION, false)
|
val copy =
|
||||||
|
directMember.copy(directMember.containingDeclaration, directMember.modality, directMember.visibility, DECLARATION, false)
|
||||||
if (isAccessor) {
|
if (isAccessor) {
|
||||||
val property = copy as PropertyDescriptor
|
val property = copy as PropertyDescriptor
|
||||||
return if (isGetter) property.getter!! else property.setter!!
|
return if (isGetter) property.getter!! else property.setter!!
|
||||||
|
|||||||
+14
-8
@@ -57,12 +57,16 @@ class InterfaceDelegationLowering(val state: GenerationState) : IrElementTransfo
|
|||||||
//skip java 8 default methods
|
//skip java 8 default methods
|
||||||
if (!interfaceFun.isDefinitelyNotDefaultImplsMethod()) {
|
if (!interfaceFun.isDefinitelyNotDefaultImplsMethod()) {
|
||||||
val inheritedFun =
|
val inheritedFun =
|
||||||
if (classDescriptor !== descriptor) {
|
if (classDescriptor !== descriptor) {
|
||||||
InterfaceLowering.createDefaultImplFunDescriptor(descriptor as DefaultImplsClassDescriptorImpl, interfaceFun, classDescriptor, state.typeMapper)
|
InterfaceLowering.createDefaultImplFunDescriptor(
|
||||||
}
|
descriptor as DefaultImplsClassDescriptorImpl,
|
||||||
else {
|
interfaceFun,
|
||||||
value
|
classDescriptor,
|
||||||
}
|
state.typeMapper
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
value
|
||||||
|
}
|
||||||
generateDelegationToDefaultImpl(irClass, interfaceFun, inheritedFun)
|
generateDelegationToDefaultImpl(irClass, interfaceFun, inheritedFun)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,9 +79,11 @@ class InterfaceDelegationLowering(val state: GenerationState) : IrElementTransfo
|
|||||||
|
|
||||||
val interfaceDescriptor = interfaceFun.containingDeclaration as ClassDescriptor
|
val interfaceDescriptor = interfaceFun.containingDeclaration as ClassDescriptor
|
||||||
val defaultImpls = InterfaceLowering.createDefaultImplsClassDescriptor(interfaceDescriptor)
|
val defaultImpls = InterfaceLowering.createDefaultImplsClassDescriptor(interfaceDescriptor)
|
||||||
val defaultImplFun = InterfaceLowering.createDefaultImplFunDescriptor(defaultImpls, interfaceFun.original, interfaceDescriptor, state.typeMapper)
|
val defaultImplFun =
|
||||||
|
InterfaceLowering.createDefaultImplFunDescriptor(defaultImpls, interfaceFun.original, interfaceDescriptor, state.typeMapper)
|
||||||
val returnType = inheritedFun.returnType!!
|
val returnType = inheritedFun.returnType!!
|
||||||
val irCallImpl = IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, defaultImplFun, null, JvmLoweredStatementOrigin.DEFAULT_IMPLS_DELEGATION)
|
val irCallImpl =
|
||||||
|
IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, defaultImplFun, null, JvmLoweredStatementOrigin.DEFAULT_IMPLS_DELEGATION)
|
||||||
irBody.statements.add(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, inheritedFun, irCallImpl))
|
irBody.statements.add(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, inheritedFun, irCallImpl))
|
||||||
|
|
||||||
var shift = 0
|
var shift = 0
|
||||||
|
|||||||
+38
-30
@@ -49,7 +49,8 @@ class InterfaceLowering(val state: GenerationState) : IrElementTransformerVoid()
|
|||||||
|
|
||||||
val interfaceDescriptor = irClass.descriptor
|
val interfaceDescriptor = irClass.descriptor
|
||||||
val defaultImplsDescriptor = createDefaultImplsClassDescriptor(interfaceDescriptor)
|
val defaultImplsDescriptor = createDefaultImplsClassDescriptor(interfaceDescriptor)
|
||||||
val defaultImplsIrClass = IrClassImpl(irClass.startOffset, irClass.endOffset, JvmLoweredDeclarationOrigin.DEFAULT_IMPLS, defaultImplsDescriptor)
|
val defaultImplsIrClass =
|
||||||
|
IrClassImpl(irClass.startOffset, irClass.endOffset, JvmLoweredDeclarationOrigin.DEFAULT_IMPLS, defaultImplsDescriptor)
|
||||||
irClass.declarations.add(defaultImplsIrClass)
|
irClass.declarations.add(defaultImplsIrClass)
|
||||||
|
|
||||||
val members = defaultImplsIrClass.declarations
|
val members = defaultImplsIrClass.declarations
|
||||||
@@ -57,7 +58,8 @@ class InterfaceLowering(val state: GenerationState) : IrElementTransformerVoid()
|
|||||||
irClass.declarations.filterIsInstance<IrFunction>().forEach {
|
irClass.declarations.filterIsInstance<IrFunction>().forEach {
|
||||||
val descriptor = it.descriptor
|
val descriptor = it.descriptor
|
||||||
if (descriptor.modality != Modality.ABSTRACT) {
|
if (descriptor.modality != Modality.ABSTRACT) {
|
||||||
val functionDescriptorImpl = createDefaultImplFunDescriptor(defaultImplsDescriptor, descriptor, interfaceDescriptor, state.typeMapper)
|
val functionDescriptorImpl =
|
||||||
|
createDefaultImplFunDescriptor(defaultImplsDescriptor, descriptor, interfaceDescriptor, state.typeMapper)
|
||||||
members.add(functionDescriptorImpl.createFunctionAndMapVariables(it))
|
members.add(functionDescriptorImpl.createFunctionAndMapVariables(it))
|
||||||
it.body = null
|
it.body = null
|
||||||
}
|
}
|
||||||
@@ -71,8 +73,7 @@ class InterfaceLowering(val state: GenerationState) : IrElementTransformerVoid()
|
|||||||
val visibility = AsmUtil.getVisibilityAccessFlag(it.descriptor)
|
val visibility = AsmUtil.getVisibilityAccessFlag(it.descriptor)
|
||||||
if (visibility == Opcodes.ACC_PRIVATE && it.descriptor.name != clinitName) {
|
if (visibility == Opcodes.ACC_PRIVATE && it.descriptor.name != clinitName) {
|
||||||
it
|
it
|
||||||
}
|
} else null
|
||||||
else null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val defaultBodies = irClass.declarations.filterIsInstance<IrFunction>().filter {
|
val defaultBodies = irClass.declarations.filterIsInstance<IrFunction>().filter {
|
||||||
@@ -86,14 +87,14 @@ class InterfaceLowering(val state: GenerationState) : IrElementTransformerVoid()
|
|||||||
|
|
||||||
fun createDefaultImplsClassDescriptor(interfaceDescriptor: ClassDescriptor): DefaultImplsClassDescriptorImpl {
|
fun createDefaultImplsClassDescriptor(interfaceDescriptor: ClassDescriptor): DefaultImplsClassDescriptorImpl {
|
||||||
return DefaultImplsClassDescriptorImpl(
|
return DefaultImplsClassDescriptorImpl(
|
||||||
Name.identifier(JvmAbi.DEFAULT_IMPLS_CLASS_NAME), interfaceDescriptor, interfaceDescriptor.source
|
Name.identifier(JvmAbi.DEFAULT_IMPLS_CLASS_NAME), interfaceDescriptor, interfaceDescriptor.source
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createDefaultImplFunDescriptor(
|
fun createDefaultImplFunDescriptor(
|
||||||
defaultImplsDescriptor: DefaultImplsClassDescriptorImpl,
|
defaultImplsDescriptor: DefaultImplsClassDescriptorImpl,
|
||||||
descriptor: FunctionDescriptor,
|
descriptor: FunctionDescriptor,
|
||||||
interfaceDescriptor: ClassDescriptor, typeMapper: KotlinTypeMapper
|
interfaceDescriptor: ClassDescriptor, typeMapper: KotlinTypeMapper
|
||||||
): SimpleFunctionDescriptorImpl {
|
): SimpleFunctionDescriptorImpl {
|
||||||
val name = Name.identifier(typeMapper.mapAsmMethod(descriptor).name)
|
val name = Name.identifier(typeMapper.mapAsmMethod(descriptor).name)
|
||||||
return createStaticFunctionWithReceivers(defaultImplsDescriptor, name, descriptor, interfaceDescriptor.defaultType)
|
return createStaticFunctionWithReceivers(defaultImplsDescriptor, name, descriptor, interfaceDescriptor.defaultType)
|
||||||
@@ -102,42 +103,49 @@ class InterfaceLowering(val state: GenerationState) : IrElementTransformerVoid()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal fun createStaticFunctionWithReceivers(owner: ClassOrPackageFragmentDescriptor, name: Name, descriptor: FunctionDescriptor, dispatchReceiverType: KotlinType): SimpleFunctionDescriptorImpl {
|
internal fun createStaticFunctionWithReceivers(
|
||||||
|
owner: ClassOrPackageFragmentDescriptor,
|
||||||
|
name: Name,
|
||||||
|
descriptor: FunctionDescriptor,
|
||||||
|
dispatchReceiverType: KotlinType
|
||||||
|
): SimpleFunctionDescriptorImpl {
|
||||||
val newFunction = SimpleFunctionDescriptorImpl.create(
|
val newFunction = SimpleFunctionDescriptorImpl.create(
|
||||||
owner,
|
owner,
|
||||||
AnnotationsImpl(emptyList()),
|
AnnotationsImpl(emptyList()),
|
||||||
name,
|
name,
|
||||||
CallableMemberDescriptor.Kind.DECLARATION, descriptor.source
|
CallableMemberDescriptor.Kind.DECLARATION, descriptor.source
|
||||||
)
|
)
|
||||||
var offset = 0
|
var offset = 0
|
||||||
val dispatchReceiver =
|
val dispatchReceiver =
|
||||||
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
|
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
|
||||||
newFunction, null, offset++, AnnotationsImpl(emptyList()), Name.identifier("this"),
|
newFunction, null, offset++, AnnotationsImpl(emptyList()), Name.identifier("this"),
|
||||||
dispatchReceiverType, false, false, false, null, descriptor.source, null)
|
dispatchReceiverType, false, false, false, null, descriptor.source, null
|
||||||
|
)
|
||||||
val extensionReceiver =
|
val extensionReceiver =
|
||||||
descriptor.extensionReceiverParameter?.let { extensionReceiver ->
|
descriptor.extensionReceiverParameter?.let { extensionReceiver ->
|
||||||
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
|
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
|
||||||
newFunction, null, offset++, AnnotationsImpl(emptyList()), Name.identifier("receiver"),
|
newFunction, null, offset++, AnnotationsImpl(emptyList()), Name.identifier("receiver"),
|
||||||
extensionReceiver.value.type, false, false, false, null, extensionReceiver.source, null)
|
extensionReceiver.value.type, false, false, false, null, extensionReceiver.source, null
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) +
|
val valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) +
|
||||||
descriptor.valueParameters.map { it.copy(newFunction, it.name, it.index + offset) }
|
descriptor.valueParameters.map { it.copy(newFunction, it.name, it.index + offset) }
|
||||||
|
|
||||||
newFunction.initialize(
|
newFunction.initialize(
|
||||||
null, null, emptyList()/*TODO: type parameters*/,
|
null, null, emptyList()/*TODO: type parameters*/,
|
||||||
valueParameters, descriptor.returnType, Modality.FINAL, descriptor.visibility
|
valueParameters, descriptor.returnType, Modality.FINAL, descriptor.visibility
|
||||||
)
|
)
|
||||||
return newFunction
|
return newFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun FunctionDescriptor.createFunctionAndMapVariables(oldFunction: IrFunction) =
|
internal fun FunctionDescriptor.createFunctionAndMapVariables(oldFunction: IrFunction) =
|
||||||
IrFunctionImpl(oldFunction.startOffset, oldFunction.endOffset, oldFunction.origin, this, oldFunction.body).also {
|
IrFunctionImpl(oldFunction.startOffset, oldFunction.endOffset, oldFunction.origin, this, oldFunction.body).also {
|
||||||
val mapping: Map<ValueDescriptor, ValueDescriptor> =
|
val mapping: Map<ValueDescriptor, ValueDescriptor> =
|
||||||
(
|
(
|
||||||
listOfNotNull(oldFunction.descriptor.dispatchReceiverParameter!!, oldFunction.descriptor.extensionReceiverParameter) +
|
listOfNotNull(oldFunction.descriptor.dispatchReceiverParameter!!, oldFunction.descriptor.extensionReceiverParameter) +
|
||||||
oldFunction.descriptor.valueParameters
|
oldFunction.descriptor.valueParameters
|
||||||
).zip(this.valueParameters).toMap()
|
).zip(this.valueParameters).toMap()
|
||||||
|
|
||||||
it.body?.transform(VariableRemapper(mapping), null)
|
it.body?.transform(VariableRemapper(mapping), null)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user