package jetbrains.mps.baseLanguage.util.plugin.refactorings;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.dataFlow.framework.Program;
import jetbrains.mps.lang.dataFlow.MPSProgramBuilder;
import jetbrains.mps.lang.dataFlow.framework.instructions.Instruction;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.dataFlow.framework.instructions.ReadInstruction;
import jetbrains.mps.lang.dataFlow.framework.instructions.WriteInstruction;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.dataFlow.framework.AnalysisResult;
import java.util.Set;
import jetbrains.mps.lang.dataFlow.framework.analyzers.ReachingReadsAnalyzer;
import jetbrains.mps.internal.collections.runtime.SetSequence;
public abstract class InlineVariableRefactoring {
public InlineVariableRefactoring() {
}
public abstract SNode doRefactoring();
public void optimizeDeclaration(SNode variable) {
Program program = new MPSProgramBuilder().buildProgram(getBaseStatementList(variable));
for (Instruction instruction : ListSequence.fromList(program.getInstructions())) {
if (instruction instanceof ReadInstruction) {
ReadInstruction read = (ReadInstruction) instruction;
if (read.getVariable() == variable) {
return;
}
}
}
for (Instruction instruction : ListSequence.fromList(program.getInstructions())) {
if (instruction instanceof WriteInstruction) {
WriteInstruction write = (WriteInstruction) instruction;
if (write.getSource() != variable && write.getVariable() == variable) {
SNodeOperations.deleteNode(SLinkOperations.getTarget(variable, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer")));
return;
}
}
}
SNodeOperations.deleteNode(SNodeOperations.getNodeAncestor(variable, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b215L, "jetbrains.mps.baseLanguage.structure.Statement"), false, false));
}
public void optimizeAssignment(SNode assignment, SNode variable) {
SNode method = SNodeOperations.getNodeAncestor(assignment, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b1fcL, "jetbrains.mps.baseLanguage.structure.BaseMethodDeclaration"), false, false);
Program program = new MPSProgramBuilder().buildProgram(SLinkOperations.getTarget(method, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b1fcL, 0xf8cc56b1ffL, "body")));
AnalysisResult<Set<ReadInstruction>> reachingReads = program.analyze(new ReachingReadsAnalyzer());
for (Instruction instruction : ListSequence.fromList(program.getInstructionsFor(assignment))) {
for (Instruction next : SetSequence.fromSet(instruction.succ())) {
for (ReadInstruction read : reachingReads.get(next)) {
if (read.getVariable() == variable) {
return;
}
}
}
}
SNodeOperations.deleteNode(SNodeOperations.getParent(assignment));
}
public SNode getBaseStatementList(SNode node) {
SNode current = SNodeOperations.getNodeAncestor(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false);
while ((SNodeOperations.getNodeAncestor(current, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false) != null)) {
current = SNodeOperations.getNodeAncestor(current, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false);
}
return current;
}
public static InlineVariableRefactoring createRefactoring(SNode node) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7efL, "jetbrains.mps.baseLanguage.structure.LocalVariableDeclaration"))) {
return new InlineVariableAssignmentRefactoring(SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7efL, "jetbrains.mps.baseLanguage.structure.LocalVariableDeclaration")));
} else {
return new InlineVariableReferenceRefactoring(SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c77f1e98L, "jetbrains.mps.baseLanguage.structure.VariableReference")));
}
}
}