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.Sets;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    
023    import java.util.Collection;
024    import java.util.LinkedHashSet;
025    
026    public abstract class InstructionImpl implements Instruction {
027        private Pseudocode owner;
028        private final Collection<Instruction> previousInstructions = new LinkedHashSet<Instruction>();
029        private final Collection<Instruction> copies = Sets.newHashSet();
030        private Instruction original;
031        protected boolean isDead = false;
032    
033        protected InstructionImpl() {
034        }
035    
036        @Override
037        @NotNull
038        public Pseudocode getOwner() {
039            return owner;
040        }
041    
042        @Override
043        public void setOwner(@NotNull Pseudocode owner) {
044            assert this.owner == null || this.owner == owner;
045            this.owner = owner;
046        }
047    
048        @NotNull
049        @Override
050        public Collection<Instruction> getPreviousInstructions() {
051            return previousInstructions;
052        }
053    
054        @Nullable
055        protected Instruction outgoingEdgeTo(@Nullable Instruction target) {
056            if (target != null) {
057                target.getPreviousInstructions().add(this);
058            }
059            return target;
060        }
061    
062        public void die() {
063            isDead = true;
064        }
065        
066        public boolean isDead() {
067            return isDead;
068        }
069    
070        public final Instruction copy() {
071            return updateCopyInfo(createCopy());
072        }
073    
074        @NotNull
075        protected abstract Instruction createCopy();
076    
077        @NotNull
078        @Override
079        public Collection<Instruction> getCopies() {
080            if (original != null) {
081                Collection<Instruction> originalCopies = Sets.newHashSet(original.getCopies());
082                originalCopies.remove(this);
083                originalCopies.add(original);
084                return originalCopies;
085            }
086            return copies;
087        }
088    
089        private void addCopy(@NotNull Instruction instruction) {
090            copies.add(instruction);
091        }
092    
093        private void setOriginal(@NotNull Instruction original) {
094            assert this.original == null :
095                    "Instruction can't have two originals: this.original = " + this.original + "; new original = " + original;
096            this.original = original;
097        }
098    
099        protected Instruction updateCopyInfo(@NotNull Instruction instruction) {
100            addCopy(instruction);
101            ((InstructionImpl)instruction).setOriginal(this);
102            return instruction;
103        }
104    }