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