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 org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction;
021 import org.jetbrains.kotlin.cfg.pseudocode.instructions.InstructionVisitorWithResult;
022 import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MagicInstruction;
023 import org.jetbrains.kotlin.cfg.pseudocode.instructions.eval.MergeInstruction;
024 import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.AbstractJumpInstruction;
025 import org.jetbrains.kotlin.cfg.pseudocode.instructions.jumps.ThrowExceptionInstruction;
026 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.MarkInstruction;
027 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineExitInstruction;
028 import org.jetbrains.kotlin.cfg.pseudocode.instructions.special.SubroutineSinkInstruction;
029 import org.jetbrains.kotlin.psi.KtElement;
030
031 /**
032 * Returns true when visited instruction may lie on a path from a tail-call-like operation to the sink of the subroutine
033 */
034 public class TailInstructionDetector extends InstructionVisitorWithResult<Boolean> {
035 private final KtElement subroutine;
036
037 public TailInstructionDetector(@NotNull KtElement subroutine) {
038 this.subroutine = subroutine;
039 }
040
041 @Override
042 public Boolean visitInstruction(@NotNull Instruction instruction) {
043 return false;
044 }
045
046 @Override
047 public Boolean visitSubroutineExit(@NotNull SubroutineExitInstruction instruction) {
048 return !instruction.isError() && instruction.getSubroutine() == subroutine;
049 }
050
051 @Override
052 public Boolean visitSubroutineSink(@NotNull SubroutineSinkInstruction instruction) {
053 return instruction.getSubroutine() == subroutine;
054 }
055
056 @Override
057 public Boolean visitJump(@NotNull AbstractJumpInstruction instruction) {
058 return true;
059 }
060
061 @Override
062 public Boolean visitThrowExceptionInstruction(@NotNull ThrowExceptionInstruction instruction) {
063 return false;
064 }
065
066 @Override
067 public Boolean visitMarkInstruction(@NotNull MarkInstruction instruction) {
068 return true;
069 }
070
071 @Override
072 public Boolean visitMagic(@NotNull MagicInstruction instruction) {
073 return instruction.getSynthetic();
074 }
075
076 @Override
077 public Boolean visitMerge(@NotNull MergeInstruction instruction) {
078 return true;
079 }
080 }