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