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.pseudocode;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.cfg.Label;
021
022 import java.util.Arrays;
023 import java.util.Collection;
024
025 public class ConditionalJumpInstruction extends AbstractJumpInstruction {
026 private final boolean onTrue;
027 private Instruction nextOnTrue;
028 private Instruction nextOnFalse;
029
030 public ConditionalJumpInstruction(boolean onTrue, Label targetLabel) {
031 super(targetLabel);
032 this.onTrue = onTrue;
033 }
034
035 public boolean onTrue() {
036 return onTrue;
037 }
038
039 public Instruction getNextOnTrue() {
040 return nextOnTrue;
041 }
042
043 public void setNextOnTrue(Instruction nextOnTrue) {
044 this.nextOnTrue = outgoingEdgeTo(nextOnTrue);
045 }
046
047 public Instruction getNextOnFalse() {
048 return nextOnFalse;
049 }
050
051 public void setNextOnFalse(Instruction nextOnFalse) {
052 this.nextOnFalse = outgoingEdgeTo(nextOnFalse);
053 }
054
055 @NotNull
056 @Override
057 public Collection<Instruction> getNextInstructions() {
058 return Arrays.asList(getNextOnFalse(), getNextOnTrue());
059 }
060
061 @Override
062 public void accept(InstructionVisitor visitor) {
063 visitor.visitConditionalJump(this);
064 }
065
066 @Override
067 public String toString() {
068 String instr = onTrue ? "jt" : "jf";
069 return instr + "(" + getTargetLabel().getName() + ")";
070 }
071
072 @Override
073 protected AbstractJumpInstruction createCopy(@NotNull Label newLabel) {
074 return new ConditionalJumpInstruction(onTrue, newLabel);
075 }
076 }