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.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
026 public class RemapVisitor extends MethodBodyVisitor {
027 private final LocalVarRemapper remapper;
028 private final FieldRemapper nodeRemapper;
029 private final InstructionAdapter instructionAdapter;
030
031 public RemapVisitor(
032 @NotNull MethodVisitor mv,
033 @NotNull LocalVarRemapper remapper,
034 @NotNull FieldRemapper nodeRemapper
035 ) {
036 super(mv);
037 this.instructionAdapter = new InstructionAdapter(mv);
038 this.remapper = remapper;
039 this.nodeRemapper = nodeRemapper;
040 }
041
042 @Override
043 public void visitIincInsn(int var, int increment) {
044 remapper.visitIincInsn(var, increment, mv);
045 }
046
047 @Override
048 public void visitVarInsn(int opcode, int var) {
049 remapper.visitVarInsn(opcode, var, instructionAdapter);
050 }
051
052 @Override
053 public void visitLocalVariable(
054 @NotNull String name, @NotNull String desc, String signature, @NotNull Label start, @NotNull Label end, int index
055 ) {
056 remapper.visitLocalVariable(name, desc, signature, start, end, index, mv);
057 }
058
059 @Override
060 public void visitFieldInsn(int opcode, @NotNull String owner, @NotNull String name, @NotNull String desc) {
061 if (name.startsWith("$$$") && (nodeRemapper instanceof RegeneratedLambdaFieldRemapper || nodeRemapper.isRoot())) {
062 FieldInsnNode fin = new FieldInsnNode(opcode, owner, name, desc);
063 StackValue inline = nodeRemapper.getFieldForInline(fin, null);
064 assert inline != null : "Captured field should have not null stackValue " + fin;
065 inline.put(inline.type, this);
066 return;
067 }
068 super.visitFieldInsn(opcode, owner, name, desc);
069 }
070 }