001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.codegen.inline; 018 019 import org.jetbrains.annotations.NotNull; 020 import org.jetbrains.annotations.Nullable; 021 import org.jetbrains.kotlin.codegen.StackValue; 022 import org.jetbrains.org.objectweb.asm.Opcodes; 023 import org.jetbrains.org.objectweb.asm.Type; 024 import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode; 025 026 import java.util.Collection; 027 import java.util.Map; 028 029 public class RegeneratedLambdaFieldRemapper extends FieldRemapper { 030 private final String oldOwnerType; 031 private final String newOwnerType; 032 private final Parameters parameters; 033 private final Map<String, LambdaInfo> recapturedLambdas; 034 private final boolean isConstructor; 035 036 public RegeneratedLambdaFieldRemapper( 037 String oldOwnerType, 038 String newOwnerType, 039 Parameters parameters, 040 Map<String, LambdaInfo> recapturedLambdas, 041 FieldRemapper remapper, 042 boolean isConstructor 043 ) { 044 super(oldOwnerType, remapper, parameters); 045 this.oldOwnerType = oldOwnerType; 046 this.newOwnerType = newOwnerType; 047 this.parameters = parameters; 048 this.recapturedLambdas = recapturedLambdas; 049 this.isConstructor = isConstructor; 050 } 051 052 @Override 053 public boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) { 054 return super.canProcess(fieldOwner, fieldName, isFolding) || isRecapturedLambdaType(fieldOwner, isFolding); 055 } 056 057 private boolean isRecapturedLambdaType(String owner, boolean isFolding) { 058 return recapturedLambdas.containsKey(owner) && (isFolding || false == parent instanceof InlinedLambdaRemapper); 059 } 060 061 @Nullable 062 @Override 063 public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) { 064 boolean searchInParent = !canProcess(fieldInsnNode.owner, fieldInsnNode.name, false); 065 if (searchInParent) { 066 return parent.findField(fieldInsnNode); 067 } else { 068 return findFieldInMyCaptured(fieldInsnNode); 069 } 070 } 071 072 @Override 073 public boolean processNonAload0FieldAccessChains(boolean isInlinedLambda) { 074 return isInlinedLambda && isConstructor; 075 } 076 077 @Nullable 078 public CapturedParamInfo findFieldInMyCaptured(@NotNull FieldInsnNode fieldInsnNode) { 079 return super.findField(fieldInsnNode, parameters.getCaptured()); 080 } 081 082 @Override 083 public String getNewLambdaInternalName() { 084 return newOwnerType; 085 } 086 087 @Nullable 088 @Override 089 public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) { 090 assert node.name.startsWith("$$$") : "Captured field template should start with $$$ prefix"; 091 if (node.name.equals("$$$" + InlineCodegenUtil.THIS)) { 092 assert oldOwnerType.equals(node.owner) : "Can't unfold '$$$THIS' parameter"; 093 return StackValue.LOCAL_0; 094 } 095 096 FieldInsnNode fin = new FieldInsnNode(node.getOpcode(), node.owner, node.name.substring(3), node.desc); 097 CapturedParamInfo field = findFieldInMyCaptured(fin); 098 099 boolean searchInParent = false; 100 if (field == null) { 101 field = findFieldInMyCaptured(new FieldInsnNode(Opcodes.GETSTATIC, oldOwnerType, "this$0", Type.getObjectType(parent.getLambdaInternalName()).getDescriptor())); 102 searchInParent = true; 103 if (field == null) { 104 throw new IllegalStateException("Couldn't find captured this " + getLambdaInternalName() + " for " + node.name); 105 } 106 } 107 108 StackValue result = StackValue.field(field.isSkipped ? 109 Type.getObjectType(parent.parent.getNewLambdaInternalName()) : field.getType(), 110 Type.getObjectType(getNewLambdaInternalName()), /*TODO owner type*/ 111 field.getNewFieldName(), false, 112 prefix == null ? StackValue.LOCAL_0 : prefix); 113 114 return searchInParent ? parent.getFieldForInline(node, result) : result; 115 } 116 }