Convert RegeneratedLambdaFieldRemapper to Kotlin

This commit is contained in:
Mikhael Bogdanov
2017-05-16 09:57:08 +02:00
parent fe4668c315
commit eca359f176
@@ -14,108 +14,76 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.codegen.inline; package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.annotations.Nullable; import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.kotlin.codegen.StackValue; import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.Opcodes; import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode
import org.jetbrains.org.objectweb.asm.Type;
import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode;
import java.util.Collection; class RegeneratedLambdaFieldRemapper(
import java.util.Map; val oldOwnerType: String,
override val newLambdaInternalName: String,
private val parameters: Parameters,
private val recapturedLambdas: Map<String, LambdaInfo>,
remapper: FieldRemapper,
private val isConstructor: Boolean
) : FieldRemapper(oldOwnerType, remapper, parameters) {
public class RegeneratedLambdaFieldRemapper extends FieldRemapper { public override fun canProcess(fieldOwner: String, fieldName: String, isFolding: Boolean) =
private final String oldOwnerType; super.canProcess(fieldOwner, fieldName, isFolding) || isRecapturedLambdaType(fieldOwner, isFolding)
private final String newOwnerType;
private final Parameters parameters;
private final Map<String, LambdaInfo> recapturedLambdas;
private final boolean isConstructor;
public RegeneratedLambdaFieldRemapper( private fun isRecapturedLambdaType(owner: String, isFolding: Boolean) =
@NotNull String oldOwnerType, recapturedLambdas.containsKey(owner) && (isFolding || parent !is InlinedLambdaRemapper)
@NotNull String newOwnerType,
@NotNull Parameters parameters,
@NotNull Map<String, LambdaInfo> recapturedLambdas,
@NotNull FieldRemapper remapper,
boolean isConstructor
) {
super(oldOwnerType, remapper, parameters);
this.oldOwnerType = oldOwnerType;
this.newOwnerType = newOwnerType;
this.parameters = parameters;
this.recapturedLambdas = recapturedLambdas;
this.isConstructor = isConstructor;
}
@Override override fun findField(fieldInsnNode: FieldInsnNode, captured: Collection<CapturedParamInfo>): CapturedParamInfo? {
public boolean canProcess(@NotNull String fieldOwner, @NotNull String fieldName, boolean isFolding) { val searchInParent = !canProcess(fieldInsnNode.owner, fieldInsnNode.name, false)
return super.canProcess(fieldOwner, fieldName, isFolding) || isRecapturedLambdaType(fieldOwner, isFolding);
}
private boolean isRecapturedLambdaType(@NotNull String owner, boolean isFolding) {
return recapturedLambdas.containsKey(owner) && (isFolding || !(parent instanceof InlinedLambdaRemapper));
}
@Nullable
@Override
public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<? extends CapturedParamInfo> captured) {
boolean searchInParent = !canProcess(fieldInsnNode.owner, fieldInsnNode.name, false);
if (searchInParent) { if (searchInParent) {
return parent.findField(fieldInsnNode); return parent!!.findField(fieldInsnNode)
} }
return findFieldInMyCaptured(fieldInsnNode); return findFieldInMyCaptured(fieldInsnNode)
} }
@Override override fun processNonAload0FieldAccessChains(isInlinedLambda: Boolean): Boolean {
public boolean processNonAload0FieldAccessChains(boolean isInlinedLambda) { return isInlinedLambda && isConstructor
return isInlinedLambda && isConstructor;
} }
@Nullable private fun findFieldInMyCaptured(fieldInsnNode: FieldInsnNode): CapturedParamInfo? {
private CapturedParamInfo findFieldInMyCaptured(@NotNull FieldInsnNode fieldInsnNode) { return super.findField(fieldInsnNode, parameters.captured)
return super.findField(fieldInsnNode, parameters.getCaptured());
} }
@NotNull override fun getFieldForInline(node: FieldInsnNode, prefix: StackValue?): StackValue? {
@Override assert(node.name.startsWith("$$$")) { "Captured field template should start with $$$ prefix" }
public String getNewLambdaInternalName() { if (node.name == "$$$" + InlineCodegenUtil.THIS) {
return newOwnerType; assert(oldOwnerType == node.owner) { "Can't unfold '$$\$THIS' parameter" }
} return StackValue.LOCAL_0
@Nullable
@Override
public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) {
assert node.name.startsWith("$$$") : "Captured field template should start with $$$ prefix";
if (node.name.equals("$$$" + InlineCodegenUtil.THIS)) {
assert oldOwnerType.equals(node.owner) : "Can't unfold '$$$THIS' parameter";
return StackValue.LOCAL_0;
} }
FieldInsnNode fin = new FieldInsnNode(node.getOpcode(), node.owner, node.name.substring(3), node.desc); val fin = FieldInsnNode(node.opcode, node.owner, node.name.substring(3), node.desc)
CapturedParamInfo field = findFieldInMyCaptured(fin); var field = findFieldInMyCaptured(fin)
boolean searchInParent = false; var searchInParent = false
if (field == null) { if (field == null) {
field = findFieldInMyCaptured(new FieldInsnNode( field = findFieldInMyCaptured(FieldInsnNode(
Opcodes.GETSTATIC, oldOwnerType, InlineCodegenUtil.THIS$0, Opcodes.GETSTATIC, oldOwnerType, InlineCodegenUtil.`THIS$0`,
Type.getObjectType(parent.getLambdaInternalName()).getDescriptor() Type.getObjectType(parent!!.lambdaInternalName!!).descriptor
)); ))
searchInParent = true; searchInParent = true
if (field == null) { if (field == null) {
throw new IllegalStateException("Couldn't find captured this " + getLambdaInternalName() + " for " + node.name); throw IllegalStateException("Couldn't find captured this " + lambdaInternalName + " for " + node.name)
} }
} }
StackValue result = StackValue.field( val result = StackValue.field(
field.isSkipped ? if (field.isSkipped)
Type.getObjectType(parent.parent.getNewLambdaInternalName()) : field.getType(), Type.getObjectType(parent!!.parent!!.newLambdaInternalName)
Type.getObjectType(getNewLambdaInternalName()), /*TODO owner type*/ else
field.getNewFieldName(), false, field.getType(),
prefix == null ? StackValue.LOCAL_0 : prefix Type.getObjectType(newLambdaInternalName), /*TODO owner type*/
); field.newFieldName, false,
prefix ?: StackValue.LOCAL_0
)
return searchInParent ? parent.getFieldForInline(node, result) : result; return if (searchInParent) parent!!.getFieldForInline(node, result) else result
} }
} }