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.asm4.Opcodes;
022    import org.jetbrains.asm4.tree.AbstractInsnNode;
023    import org.jetbrains.asm4.tree.FieldInsnNode;
024    import org.jetbrains.asm4.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        public boolean canProcess(@NotNull String fieldOwner, boolean isFolding) {
045            return fieldOwner.equals(getLambdaInternalName());
046        }
047    
048        @Nullable
049        public AbstractInsnNode foldFieldAccessChainIfNeeded(
050                @NotNull List<AbstractInsnNode> capturedFieldAccess,
051                @NotNull MethodNode node
052        ) {
053            if (capturedFieldAccess.size() == 1) {
054                //just aload
055                return null;
056            }
057    
058            return foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node);
059        }
060    
061        @Nullable
062        private AbstractInsnNode foldFieldAccessChainIfNeeded(
063                @NotNull List<AbstractInsnNode> capturedFieldAccess,
064                int currentInstruction,
065                @NotNull MethodNode node
066        ) {
067            AbstractInsnNode transformed = null;
068            boolean checkParent = !isRoot() && currentInstruction < capturedFieldAccess.size() - 1;
069            if (checkParent) {
070                transformed = parent.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node);
071            }
072    
073            if (transformed == null) {
074                //if parent couldn't transform
075                FieldInsnNode insnNode = (FieldInsnNode) capturedFieldAccess.get(currentInstruction);
076                if (canProcess(insnNode.owner, true)) {
077                    insnNode.name = "$$$" + insnNode.name;
078                    insnNode.setOpcode(Opcodes.GETSTATIC);
079    
080                    AbstractInsnNode next = capturedFieldAccess.get(0);
081                    while (next != insnNode) {
082                        AbstractInsnNode toDelete = next;
083                        next = next.getNext();
084                        node.instructions.remove(toDelete);
085                    }
086    
087                    transformed = capturedFieldAccess.get(capturedFieldAccess.size() - 1);
088                }
089            }
090    
091            return transformed;
092        }
093    
094        public CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode) {
095            return findField(fieldInsnNode, params.getCaptured());
096        }
097    
098        @Nullable
099        protected CapturedParamInfo findField(@NotNull FieldInsnNode fieldInsnNode, @NotNull Collection<CapturedParamInfo> captured) {
100            for (CapturedParamInfo valueDescriptor : captured) {
101                if (valueDescriptor.getOriginalFieldName().equals(fieldInsnNode.name) && fieldInsnNode.owner.equals(valueDescriptor.getContainingLambdaName())) {
102                    return valueDescriptor;
103                }
104            }
105            return null;
106        }
107    
108        public FieldRemapper getParent() {
109            return parent;
110        }
111    
112        public String getLambdaInternalName() {
113            return lambdaInternalName;
114        }
115    
116        public boolean isRoot() {
117            return parent == null;
118        }
119    
120        @Nullable
121        public StackValue getFieldForInline(@NotNull FieldInsnNode node, @Nullable StackValue prefix) {
122            CapturedParamInfo field = MethodInliner.findCapturedField(node, this);
123            return field.getRemapValue();
124        }
125    
126        public boolean isInsideInliningLambda() {
127            return !isRoot() && parent.isInsideInliningLambda();
128        }
129    }