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.lang.cfg;
018
019 import kotlin.Function1;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.jet.lang.cfg.pseudocode.*;
022 import org.jetbrains.jet.lang.psi.JetElement;
023
024 public class TailRecursionDetector extends InstructionVisitorWithResult<Boolean> implements Function1<Instruction, Boolean> {
025 private final JetElement subroutine;
026 private final Instruction start;
027
028 public TailRecursionDetector(@NotNull JetElement subroutine, @NotNull Instruction start) {
029 this.subroutine = subroutine;
030 this.start = start;
031 }
032
033 @Override
034 public Boolean invoke(@NotNull Instruction instruction) {
035 return instruction == start || instruction.accept(this);
036 }
037
038 @Override
039 public Boolean visitInstruction(Instruction instruction) {
040 return false;
041 }
042
043 @Override
044 public Boolean visitSubroutineExit(SubroutineExitInstruction instruction) {
045 return !instruction.isError() && instruction.getSubroutine() == subroutine;
046 }
047
048 @Override
049 public Boolean visitSubroutineSink(SubroutineSinkInstruction instruction) {
050 return instruction.getSubroutine() == subroutine;
051 }
052
053 @Override
054 public Boolean visitJump(AbstractJumpInstruction instruction) {
055 return true;
056 }
057
058 @Override
059 public Boolean visitThrowExceptionInstruction(ThrowExceptionInstruction instruction) {
060 return false;
061 }
062
063 @Override
064 public Boolean visitMarkInstruction(MarkInstruction instruction) {
065 return true;
066 }
067 }