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 035 public RegeneratedLambdaFieldRemapper( 036 String oldOwnerType, 037 String newOwnerType, 038 Parameters parameters, 039 Map<String, LambdaInfo> recapturedLambdas, 040 FieldRemapper remapper 041 ) { 042 super(oldOwnerType, remapper, parameters); 043 this.oldOwnerType = oldOwnerType; 044 this.newOwnerType = newOwnerType; 045 this.parameters = parameters; 046 this.recapturedLambdas = recapturedLambdas; 047 } 048 049 @Override 050 public boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) { 051 return super.canProcess(fieldOwner, fieldName, isFolding) || isRecapturedLambdaType(fieldOwner); 052 } 053 054 private boolean isRecapturedLambdaType(String owner) { 055 return recapturedLambdas.containsKey(owner); 056 } 057 058 @Nullable 059 @Override 060 public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) { 061 boolean searchInParent = !canProcess(fieldInsnNode.owner, fieldInsnNode.name, false); 062 if (searchInParent) { 063 return parent.findField(fieldInsnNode); 064 } else { 065 return findFieldInMyCaptured(fieldInsnNode); 066 } 067 } 068 069 @Nullable 070 public CapturedParamInfo findFieldInMyCaptured(@NotNull FieldInsnNode fieldInsnNode) { 071 return super.findField(fieldInsnNode, parameters.getCaptured()); 072 } 073 074 @Nullable 075 @Override 076 public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) { 077 assert node.name.startsWith("$$$") : "Captured field template should start with $$$ prefix"; 078 FieldInsnNode fin = new FieldInsnNode(node.getOpcode(), node.owner, node.name.substring(3), node.desc); 079 CapturedParamInfo field = findFieldInMyCaptured(fin); 080 081 boolean searchInParent = false; 082 if (field == null) { 083 field = findFieldInMyCaptured(new FieldInsnNode(Opcodes.GETSTATIC, oldOwnerType, "this$0", Type.getObjectType(parent.getLambdaInternalName()).getDescriptor())); 084 searchInParent = true; 085 if (field == null) { 086 throw new IllegalStateException("Couldn't find captured this " + getLambdaInternalName() + " for " + node.name); 087 } 088 } 089 090 StackValue result = StackValue.field(field.getType(), 091 Type.getObjectType(newOwnerType), /*TODO owner type*/ 092 field.getNewFieldName(), false, 093 prefix == null ? StackValue.LOCAL_0 : prefix); 094 095 return searchInParent ? parent.getFieldForInline(node, result) : result; 096 } 097 }