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.cfg; 018 019 import kotlin.jvm.functions.Function1; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction; 022 import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitorWithResult; 023 import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction; 024 import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MergeInstruction; 025 import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.AbstractJumpInstruction; 026 import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.ThrowExceptionInstruction; 027 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.MarkInstruction; 028 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction; 029 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction; 030 import org.jetbrains.kotlin.cfg.pseudocodeTraverser.TraverseInstructionResult; 031 import org.jetbrains.kotlin.psi.KtElement; 032 033 public class TailRecursionDetector extends InstructionVisitorWithResult<Boolean> implements Function1<Instruction, TraverseInstructionResult> { 034 private final KtElement subroutine; 035 private final Instruction start; 036 037 public TailRecursionDetector(@NotNull KtElement subroutine, @NotNull Instruction start) { 038 this.subroutine = subroutine; 039 this.start = start; 040 } 041 042 @Override 043 public TraverseInstructionResult invoke(@NotNull Instruction instruction) { 044 return instruction == start || instruction.accept(this) ? TraverseInstructionResult.CONTINUE : TraverseInstructionResult.HALT; 045 } 046 047 @Override 048 public Boolean visitInstruction(@NotNull Instruction instruction) { 049 return false; 050 } 051 052 @Override 053 public Boolean visitSubroutineExit(@NotNull SubroutineExitInstruction instruction) { 054 return !instruction.isError() && instruction.getSubroutine() == subroutine; 055 } 056 057 @Override 058 public Boolean visitSubroutineSink(@NotNull SubroutineSinkInstruction instruction) { 059 return instruction.getSubroutine() == subroutine; 060 } 061 062 @Override 063 public Boolean visitJump(@NotNull AbstractJumpInstruction instruction) { 064 return true; 065 } 066 067 @Override 068 public Boolean visitThrowExceptionInstruction(@NotNull ThrowExceptionInstruction instruction) { 069 return false; 070 } 071 072 @Override 073 public Boolean visitMarkInstruction(@NotNull MarkInstruction instruction) { 074 return true; 075 } 076 077 @Override 078 public Boolean visitMagic(@NotNull MagicInstruction instruction) { 079 return instruction.getSynthetic(); 080 } 081 082 @Override 083 public Boolean visitMerge(@NotNull MergeInstruction instruction) { 084 return true; 085 } 086 }