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