Generalizing

This commit is contained in:
Oleg Shpynov
2010-04-05 17:25:45 +04:00
parent 4b1849ba21
commit 440183c8e4
7 changed files with 84 additions and 17 deletions
@@ -39,18 +39,18 @@ public class DFAEngine<E> {
}
public List<DFAMap<E>> performDFA() {
final ArrayList<DFAMap<E>> info = new ArrayList<DFAMap<E>>(myFlow.length);
public List<E> performDFA() {
final ArrayList<E> info = new ArrayList<E>(myFlow.length);
return performDFA(info);
}
public List<DFAMap<E>> performDFA(final List<DFAMap<E>> info) {
public List<E> performDFA(final List<E> info) {
if (LOG.isDebugEnabled()){
LOG.debug("Perfoming DFA\n" + "Instance: " + myDfa + " Semilattice: " + mySemilattice);
}
// initializing dfa
final DFAMap<E> initial = myDfa.initial();
final E initial = myDfa.initial();
for (int i = 0; i < myFlow.length; i++) {
info.add(i, initial);
}
@@ -104,12 +104,12 @@ public class DFAEngine<E> {
}
final int currentNumber = currentInstruction.num();
final DFAMap<E> oldE = info.get(currentNumber);
final DFAMap<E> joinedE = join(currentInstruction, info);
final DFAMap<E> newE = myDfa.fun(joinedE, currentInstruction);
final E oldE = info.get(currentNumber);
final E joinedE = join(currentInstruction, info);
final E newE = myDfa.fun(joinedE, currentInstruction);
if (!mySemilattice.eq(newE, oldE)) {
if (LOG.isDebugEnabled()){
LOG.debug("Number: " + currentNumber + " old: " + oldE.keySet() + " new: " + newE.keySet());
LOG.debug("Number: " + currentNumber + " old: " + oldE.toString() + " new: " + newE.toString());
}
info.set(currentNumber, newE);
for (Instruction next : getNext(currentInstruction)) {
@@ -148,9 +148,9 @@ public class DFAEngine<E> {
return allPred * 2;
}
private DFAMap<E> join(final Instruction instruction, final List<DFAMap<E>> info) {
private E join(final Instruction instruction, final List<E> info) {
final Iterable<? extends Instruction> prev = myDfa.isForward() ? instruction.allPred() : instruction.allSucc();
final ArrayList<DFAMap<E>> prevInfos = new ArrayList<DFAMap<E>>();
final ArrayList<E> prevInfos = new ArrayList<E>();
for (Instruction i : prev) {
prevInfos.add(info.get(i.num()));
}
@@ -21,10 +21,10 @@ public interface DfaInstance<E> {
// Please ensure that E has correctly implemented equals method
// Invariant: fun must create new instance of DFAMap if modifies it
DFAMap<E> fun(DFAMap<E> e, Instruction instruction);
E fun(E e, Instruction instruction);
@NotNull
DFAMap<E> initial();
E initial();
boolean isForward();
}
@@ -17,9 +17,7 @@ package com.intellij.codeInsight.dataflow;
import java.util.ArrayList;
public interface Semilattice<E> {
// Invariant: join can return unmodified ins(0) or empty DFAMap
// DfaInstance must create new one before modifying
DFAMap<E> join(ArrayList<DFAMap<E>> ins);
E join(ArrayList<E> ins);
boolean eq(DFAMap<E> e1, DFAMap<E> e2);
boolean eq(E e1, E e2);
}
@@ -1,5 +1,6 @@
package com.intellij.codeInsight.dataflow;
package com.intellij.codeInsight.dataflow.map;
import com.intellij.codeInsight.dataflow.SetUtil;
import org.jetbrains.annotations.Nullable;
import java.util.*;
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.dataflow.map;
import com.intellij.codeInsight.controlflow.Instruction;
import com.intellij.codeInsight.dataflow.DFAEngine;
public class DFAMapEngine<E> extends DFAEngine<DFAMap<E>>{
public DFAMapEngine(final Instruction[] flow, final DfaMapInstance<E> dfa, final MapSemilattice<E> dfaMapSemilattice) {
super(flow, dfa, dfaMapSemilattice);
}
}
@@ -0,0 +1,21 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.dataflow.map;
import com.intellij.codeInsight.dataflow.DfaInstance;
public interface DfaMapInstance<E> extends DfaInstance<DFAMap<E>> {
}
@@ -0,0 +1,22 @@
/*
* Copyright 2000-2007 JetBrains s.r.o.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.dataflow.map;
import com.intellij.codeInsight.dataflow.Semilattice;
public interface MapSemilattice<E> extends Semilattice<DFAMap<E>>{
// Invariant: join can return unmodified ins(0) or empty DFAMap
// DfaInstance must create new one before modifying
}