001 /*
002 * Copyright 2010-2013 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.jet.codegen.inline;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.org.objectweb.asm.Opcodes;
022 import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode;
023 import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode;
024 import org.jetbrains.org.objectweb.asm.tree.MethodNode;
025 import org.jetbrains.jet.codegen.StackValue;
026
027 import java.util.Collection;
028 import java.util.List;
029
030 public class FieldRemapper {
031
032 private final String lambdaInternalName;
033
034 protected FieldRemapper parent;
035
036 private final Parameters params;
037
038 public FieldRemapper(@Nullable String lambdaInternalName, @Nullable FieldRemapper parent, @NotNull Parameters methodParams) {
039 this.lambdaInternalName = lambdaInternalName;
040 this.parent = parent;
041 params = methodParams;
042 }
043
044 protected boolean canProcess(@NotNull String fieldOwner, String fieldName, boolean isFolding) {
045 return fieldOwner.equals(getLambdaInternalName()) &&
046 //don't process general field of anonymous objects
047 InlineCodegenUtil.isCapturedFieldName(fieldName);
048 }
049
050 @Nullable
051 public AbstractInsnNode foldFieldAccessChainIfNeeded(
052 @NotNull List<AbstractInsnNode> capturedFieldAccess,
053 @NotNull MethodNode node
054 ) {
055 if (capturedFieldAccess.size() == 1) {
056 //just aload
057 return null;
058 }
059
060 return foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node);
061 }
062
063 @Nullable
064 private AbstractInsnNode foldFieldAccessChainIfNeeded(
065 @NotNull List<AbstractInsnNode> capturedFieldAccess,
066 int currentInstruction,
067 @NotNull MethodNode node
068 ) {
069 AbstractInsnNode transformed = null;
070 boolean checkParent = !isRoot() && currentInstruction < capturedFieldAccess.size() - 1;
071 if (checkParent) {
072 transformed = parent.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node);
073 }
074
075 if (transformed == null) {
076 //if parent couldn't transform
077 FieldInsnNode insnNode = (FieldInsnNode) capturedFieldAccess.get(currentInstruction);
078 if (canProcess(insnNode.owner, insnNode.name, true)) {
079 insnNode.name = "$$$" + insnNode.name;
080 insnNode.setOpcode(Opcodes.GETSTATIC);
081
082 AbstractInsnNode next = capturedFieldAccess.get(0);
083 while (next != insnNode) {
084 AbstractInsnNode toDelete = next;
085 next = next.getNext();
086 node.instructions.remove(toDelete);
087 }
088
089 transformed = capturedFieldAccess.get(capturedFieldAccess.size() - 1);
090 }
091 }
092
093 return transformed;
094 }
095
096 public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode) {
097 return findField(fieldInsnNode, params.getCaptured());
098 }
099
100 @Nullable
101 protected CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) {
102 for (CapturedParamInfo valueDescriptor : captured) {
103 if (valueDescriptor.getOriginalFieldName().equals(fieldInsnNode.name) && fieldInsnNode.owner.equals(valueDescriptor.getContainingLambdaName())) {
104 return valueDescriptor;
105 }
106 }
107 return null;
108 }
109
110 public FieldRemapper getParent() {
111 return parent;
112 }
113
114 public String getLambdaInternalName() {
115 return lambdaInternalName;
116 }
117
118 public boolean isRoot() {
119 return parent == null;
120 }
121
122 @Nullable
123 public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) {
124 CapturedParamInfo field = MethodInliner.findCapturedField(node, this);
125 return field.getRemapValue();
126 }
127
128 public boolean isInsideInliningLambda() {
129 return !isRoot() && parent.isInsideInliningLambda();
130 }
131 }