refactoring

This commit is contained in:
Igor Chevdar
2017-02-21 17:46:20 +03:00
parent bb82109ded
commit eced9bb11a
4 changed files with 56 additions and 42 deletions
@@ -70,9 +70,7 @@ internal class ClassVtablesBuilder(val classDescriptor: ClassDescriptor, val con
if (overridingMethod == null) { if (overridingMethod == null) {
superMethod superMethod
} else { } else {
// Add all possible (descriptor, overriddenDescriptor) edges for now, redundant will be removed later.
newVtableSlots.add(OverriddenFunctionDescriptor(overridingMethod, superMethod.descriptor)) newVtableSlots.add(OverriddenFunctionDescriptor(overridingMethod, superMethod.descriptor))
newVtableSlots.add(OverriddenFunctionDescriptor(overridingMethod, overridingMethod))
OverriddenFunctionDescriptor(overridingMethod, superMethod.overriddenDescriptor) OverriddenFunctionDescriptor(overridingMethod, superMethod.overriddenDescriptor)
} }
} }
@@ -87,13 +85,14 @@ internal class ClassVtablesBuilder(val classDescriptor: ClassDescriptor, val con
} }
println() println()
val zzz = inheritedVtableSlots.map { it.descriptor to it.bridgeDirections }.toSet()
// Add all possible (descriptor, overriddenDescriptor) edges for now, redundant will be removed later.
methods.mapTo(newVtableSlots) { OverriddenFunctionDescriptor(it, it) } methods.mapTo(newVtableSlots) { OverriddenFunctionDescriptor(it, it) }
val inheritedVtableSlotsSet = inheritedVtableSlots.map { it.descriptor to it.bridgeDirections }.toSet()
val filteredNewVtableSlots = newVtableSlots val filteredNewVtableSlots = newVtableSlots
.filter { !zzz.contains(it.descriptor to it.bridgeDirections) } .filterNot { inheritedVtableSlotsSet.contains(it.descriptor to it.bridgeDirections) }
.distinctBy { it.descriptor to it.bridgeDirections } .distinctBy { it.descriptor to it.bridgeDirections }
.filter { it.descriptor.isOverridable } .filter { it.descriptor.isOverridable }
println("VTABLE_ENTRIES new vtable:") println("VTABLE_ENTRIES new vtable:")
@@ -126,11 +125,12 @@ internal class ClassVtablesBuilder(val classDescriptor: ClassDescriptor, val con
fun vtableIndex(function: FunctionDescriptor): Int { fun vtableIndex(function: FunctionDescriptor): Int {
val target = function.target val target = function.target
val bridgeDirections = target.bridgeDirectionsTo(function.original)
println("VTABLE_INDEX function: $function") println("VTABLE_INDEX function: $function")
println("VTABLE_INDEX original: ${function.original}") println("VTABLE_INDEX original: ${function.original}")
println("VTABLE_INDEX target: ${target}") println("VTABLE_INDEX target: ${target}")
println("VTABLE_INDEX bridgeDirections: ${target.bridgeDirectionsTo(function.original).toString()}") println("VTABLE_INDEX bridgeDirections: ${target.bridgeDirectionsTo(function.original).toString()}")
val index = vtableEntries.indexOfFirst { it.descriptor == function.original && it.bridgeDirections == target.bridgeDirectionsTo(function.original) } val index = vtableEntries.indexOfFirst { it.descriptor == function.original && it.bridgeDirections == bridgeDirections }
if (index < 0) throw Error(function.toString() + " not in vtable of " + classDescriptor.toString()) if (index < 0) throw Error(function.toString() + " not in vtable of " + classDescriptor.toString())
return index.apply { println("VTABLE_INDEX index: $this"); println() } return index.apply { println("VTABLE_INDEX index: $this"); println() }
} }
@@ -269,7 +269,10 @@ internal fun FunctionDescriptor.bridgeDirectionsTo(overriddenDescriptor: Functio
for (index in ourDirections.array.indices) for (index in ourDirections.array.indices)
ourDirections.array[index] = this.bridgeDirectionToAt(overriddenDescriptor, index) ourDirections.array[index] = this.bridgeDirectionToAt(overriddenDescriptor, index)
if (!kind.isReal && OverridingUtil.overrides(this.target, overriddenDescriptor) && ourDirections == this.target.bridgeDirectionsTo(overriddenDescriptor)) { val target = this.target
if (!kind.isReal
&& OverridingUtil.overrides(target, overriddenDescriptor)
&& ourDirections == target.bridgeDirectionsTo(overriddenDescriptor)) {
// Bridge is inherited from supers // Bridge is inherited from supers
return BridgeDirections(this.valueParameters.size) return BridgeDirections(this.valueParameters.size)
} }
@@ -181,8 +181,13 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
val target = descriptor.target val target = descriptor.target
println("IMPLEMENTATION target: ${target}") println("IMPLEMENTATION target: ${target}")
if (!needBridge) return target if (!needBridge) return target
//val bridgeOwner = if (!descriptor.kind.isReal && OverridingUtil.overrides(target, overriddenDescriptor) && !descriptor.needBridgeTo(overriddenDescriptor)) if (descriptor.bridgeDirectionsTo(overriddenDescriptor).allNotNeeded()) target else descriptor val bridgeOwner = if (!descriptor.kind.isReal
val bridgeOwner = if (!descriptor.kind.isReal && OverridingUtil.overrides(target, overriddenDescriptor) && descriptor.bridgeDirectionsTo(overriddenDescriptor).allNotNeeded()) target else descriptor && OverridingUtil.overrides(target, overriddenDescriptor)
&& descriptor.bridgeDirectionsTo(overriddenDescriptor).allNotNeeded()) {
target // Bridge is inherited from supers
} else {
descriptor
}
println("IMPLEMENTATION owner: ${bridgeOwner}") println("IMPLEMENTATION owner: ${bridgeOwner}")
return context.specialDescriptorsFactory.getBridgeDescriptor(OverriddenFunctionDescriptor(bridgeOwner, overriddenDescriptor)) return context.specialDescriptorsFactory.getBridgeDescriptor(OverriddenFunctionDescriptor(bridgeOwner, overriddenDescriptor))
} }
@@ -36,7 +36,12 @@ internal class DirectBridgesCallsLowering(val context: Context) : BodyLoweringPa
if (descriptor.kind != CallableMemberDescriptor.Kind.DELEGATION && !needBridge) if (descriptor.kind != CallableMemberDescriptor.Kind.DELEGATION && !needBridge)
return expression return expression
val toCall = if (needBridge) target else context.specialDescriptorsFactory.getBridgeDescriptor(OverriddenFunctionDescriptor(descriptor, target)) val toCall = if (needBridge) {
target
} else {
// Need to call delegating fun.
context.specialDescriptorsFactory.getBridgeDescriptor(OverriddenFunctionDescriptor(descriptor, target))
}
return IrCallImpl(expression.startOffset, expression.endOffset, return IrCallImpl(expression.startOffset, expression.endOffset,
toCall, remapTypeArguments(expression, toCall)).apply { toCall, remapTypeArguments(expression, toCall)).apply {
@@ -150,7 +155,8 @@ internal class BridgesBuilding(val context: Context) : ClassLoweringPass {
.filter { !it.bridgeDirections.allNotNeeded() } .filter { !it.bridgeDirections.allNotNeeded() }
.filter { it.canBeCalledVirtually } .filter { it.canBeCalledVirtually }
.distinctBy { it.bridgeDirections } .distinctBy { it.bridgeDirections }
.forEach { buildBridge(it, irClass) .forEach {
buildBridge(it, irClass)
} }
} }
} }