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); 055 } 056 057 private boolean isRecapturedLambdaType(String owner) { 058 return recapturedLambdas.containsKey(owner); 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 @Nullable 083 @Override 084 public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) { 085 assert node.name.startsWith("$$$") : "Captured field template should start with $$$ prefix"; 086 FieldInsnNode fin = new FieldInsnNode(node.getOpcode(), node.owner, node.name.substring(3), node.desc); 087 CapturedParamInfo field = findFieldInMyCaptured(fin); 088 089 boolean searchInParent = false; 090 if (field == null) { 091 field = findFieldInMyCaptured(new FieldInsnNode(Opcodes.GETSTATIC, oldOwnerType, "this$0", Type.getObjectType(parent.getLambdaInternalName()).getDescriptor())); 092 searchInParent = true; 093 if (field == null) { 094 throw new IllegalStateException("Couldn't find captured this " + getLambdaInternalName() + " for " + node.name); 095 } 096 } 097 098 StackValue result = StackValue.field(field.getType(), 099 Type.getObjectType(newOwnerType), /*TODO owner type*/ 100 field.getNewFieldName(), false, 101 prefix == null ? StackValue.LOCAL_0 : prefix); 102 103 return searchInParent ? parent.getFieldForInline(node, result) : result; 104 } 105 }