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.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.psi.JetElement; 031 032 public class TailRecursionDetector extends InstructionVisitorWithResult<Boolean> implements Function1<Instruction, Boolean> { 033 private final JetElement subroutine; 034 private final Instruction start; 035 036 public TailRecursionDetector(@NotNull JetElement subroutine, @NotNull Instruction start) { 037 this.subroutine = subroutine; 038 this.start = start; 039 } 040 041 @Override 042 public Boolean invoke(@NotNull Instruction instruction) { 043 return instruction == start || instruction.accept(this); 044 } 045 046 @Override 047 public Boolean visitInstruction(@NotNull Instruction instruction) { 048 return false; 049 } 050 051 @Override 052 public Boolean visitSubroutineExit(@NotNull SubroutineExitInstruction instruction) { 053 return !instruction.getIsError() && instruction.getSubroutine() == subroutine; 054 } 055 056 @Override 057 public Boolean visitSubroutineSink(@NotNull SubroutineSinkInstruction instruction) { 058 return instruction.getSubroutine() == subroutine; 059 } 060 061 @Override 062 public Boolean visitJump(@NotNull AbstractJumpInstruction instruction) { 063 return true; 064 } 065 066 @Override 067 public Boolean visitThrowExceptionInstruction(@NotNull ThrowExceptionInstruction instruction) { 068 return false; 069 } 070 071 @Override 072 public Boolean visitMarkInstruction(@NotNull MarkInstruction instruction) { 073 return true; 074 } 075 076 @Override 077 public Boolean visitMagic(@NotNull MagicInstruction instruction) { 078 return instruction.getSynthetic(); 079 } 080 081 @Override 082 public Boolean visitMerge(@NotNull MergeInstruction instruction) { 083 return true; 084 } 085 }