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 com.google.common.collect.Lists;
020    import com.google.common.collect.Maps;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.jet.lang.cfg.Label;
023    
024    import java.util.*;
025    
026    public class NondeterministicJumpInstruction extends InstructionImpl{
027        private Instruction next;
028        private final List<Label> targetLabels;
029        private final Map<Label, Instruction> resolvedTargets;
030    
031        public NondeterministicJumpInstruction(List<Label> targetLabels, LexicalScope lexicalScope) {
032            super(lexicalScope);
033            this.targetLabels = Lists.newArrayList(targetLabels);
034            resolvedTargets = Maps.newLinkedHashMap();
035        }
036    
037        public NondeterministicJumpInstruction(Label targetLabel, LexicalScope lexicalScope) {
038            this(Lists.newArrayList(targetLabel), lexicalScope);
039        }
040    
041        public List<Label> getTargetLabels() {
042            return targetLabels;
043        }
044    
045        public Map<Label, Instruction> getResolvedTargets() {
046            return resolvedTargets;
047        }
048    
049        public void setResolvedTarget(Label label, Instruction resolvedTarget) {
050            Instruction target = outgoingEdgeTo(resolvedTarget);
051            resolvedTargets.put(label, target);
052        }
053    
054        public Instruction getNext() {
055            return next;
056        }
057        public void setNext(Instruction next) {
058            this.next = outgoingEdgeTo(next);
059        }
060    
061        @Override
062        public void accept(@NotNull InstructionVisitor visitor) {
063            visitor.visitNondeterministicJump(this);
064        }
065    
066        @Override
067        public <R> R accept(@NotNull InstructionVisitorWithResult<R> visitor) {
068            return visitor.visitNondeterministicJump(this);
069        }
070    
071        @NotNull
072        @Override
073        public Collection<Instruction> getNextInstructions() {
074            List<Instruction> targetInstructions = Lists.newArrayList(getResolvedTargets().values());
075            targetInstructions.add(getNext());
076            return targetInstructions;
077        }
078    
079        @Override
080        public String toString() {
081            StringBuilder sb = new StringBuilder();
082            sb.append("jmp?(");
083            for (Iterator<Label> iterator = targetLabels.iterator(); iterator.hasNext(); ) {
084                Label targetLabel = iterator.next();
085                sb.append(targetLabel.getName());
086                if (iterator.hasNext()) {
087                    sb.append(", ");
088                }
089            }
090            sb.append(")");
091            return sb.toString();
092        }
093    
094        @NotNull
095        @Override
096        protected Instruction createCopy() {
097            return createCopy(getTargetLabels());
098        }
099    
100        @NotNull
101        public final Instruction copy(@NotNull List<Label> newTargetLabels) {
102            return updateCopyInfo(createCopy(newTargetLabels));
103        }
104    
105        private Instruction createCopy(@NotNull List<Label> newTargetLabels) {
106            return new NondeterministicJumpInstruction(newTargetLabels, lexicalScope);
107        }
108    }