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.kotlin.codegen.StackValue;
021    import org.jetbrains.org.objectweb.asm.AnnotationVisitor;
022    import org.jetbrains.org.objectweb.asm.Label;
023    import org.jetbrains.org.objectweb.asm.MethodVisitor;
024    import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
025    import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode;
026    
027    public class RemapVisitor extends InliningInstructionAdapter {
028        private final LocalVarRemapper remapper;
029        private final FieldRemapper nodeRemapper;
030        private final InstructionAdapter instructionAdapter;
031    
032        protected RemapVisitor(
033                MethodVisitor mv,
034                LocalVarRemapper localVarRemapper,
035                FieldRemapper nodeRemapper
036        ) {
037            super(mv);
038            this.instructionAdapter = new InstructionAdapter(mv);
039            this.remapper = localVarRemapper;
040            this.nodeRemapper = nodeRemapper;
041        }
042    
043        @Override
044        public void visitIincInsn(int var, int increment) {
045            remapper.visitIincInsn(var, increment, mv);
046        }
047    
048        @Override
049        public void visitVarInsn(int opcode, int var) {
050            remapper.visitVarInsn(opcode, var, instructionAdapter);
051        }
052    
053        @Override
054        public void visitLocalVariable(
055                @NotNull String name, @NotNull String desc, String signature, @NotNull Label start, @NotNull Label end, int index
056        ) {
057            remapper.visitLocalVariable(name, desc, signature, start, end, index, mv);
058        }
059    
060        @Override
061        public void visitFieldInsn(int opcode, @NotNull String owner, @NotNull String name, @NotNull String desc) {
062            if (name.startsWith("$$$")) {
063                if (nodeRemapper instanceof RegeneratedLambdaFieldRemapper || nodeRemapper.isRoot()) {
064                    FieldInsnNode fin = new FieldInsnNode(opcode, owner, name, desc);
065                    StackValue inline = nodeRemapper.getFieldForInline(fin, null);
066                    assert inline != null : "Captured field should have not null stackValue " + fin;
067                    inline.put(inline.type, this);
068                }
069                else {
070                    super.visitFieldInsn(opcode, owner, name, desc);
071                }
072            }
073            else {
074                super.visitFieldInsn(opcode, owner, name, desc);
075            }
076        }
077    
078        @Override
079        public AnnotationVisitor visitAnnotationDefault() {
080            return null;
081        }
082    
083        @Override
084        public void visitMaxs(int maxStack, int maxLocals) {
085    
086        }
087    
088        @Override
089        public void visitEnd() {
090    
091        }
092    
093        @Override
094        public AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
095            return null;
096        }
097    
098        @Override
099        public AnnotationVisitor visitParameterAnnotation(int parameter, @NotNull String desc, boolean visible) {
100            return null;
101        }
102    }