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.Collection;
024    import java.util.Collections;
025    
026    public abstract class AbstractJumpInstruction extends JetElementInstructionImpl implements JumpInstruction {
027        private final Label targetLabel;
028        private Instruction resolvedTarget;
029    
030        public AbstractJumpInstruction(@NotNull JetElement element, Label targetLabel, LexicalScope lexicalScope) {
031            super(element, lexicalScope);
032            this.targetLabel = targetLabel;
033        }
034    
035        public Label getTargetLabel() {
036            return targetLabel;
037        }
038    
039        public Instruction getResolvedTarget() {
040            return resolvedTarget;
041        }
042    
043        @NotNull
044        @Override
045        public Collection<Instruction> getNextInstructions() {
046            return Collections.singleton(getResolvedTarget());
047        }
048    
049        public void setResolvedTarget(Instruction resolvedTarget) {
050            this.resolvedTarget = outgoingEdgeTo(resolvedTarget);
051        }
052    
053        protected abstract AbstractJumpInstruction createCopy(@NotNull Label newLabel, @NotNull LexicalScope lexicalScope);
054    
055        final public Instruction copy(@NotNull Label newLabel) {
056            return updateCopyInfo(createCopy(newLabel, lexicalScope));
057        }
058    
059        @NotNull
060        @Override
061        protected Instruction createCopy() {
062            return createCopy(targetLabel, lexicalScope);
063        }
064    }