Commit bf9d0ed9 authored by Daniil Ovchinnikov's avatar Daniil Ovchinnikov
Browse files

[groovy] dfa: move DfaInstance#initial to Semilattice

parent c561be93
Showing with 61 additions and 112 deletions
+61 -112
......@@ -70,15 +70,16 @@ public class InvalidWriteAccessSearcher {
e.add(((ReadWriteVariableInstruction)instruction).getDescriptor());
}
}
}
private static class MySemilattice implements Semilattice<MyData> {
@NotNull
@Override
public MyData initial() {
return new MyData();
}
}
private static class MySemilattice implements Semilattice<MyData> {
@NotNull
@Override
public MyData join(@NotNull List<? extends MyData> ins) {
......
......@@ -6,9 +6,9 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.VariableDescriptorFactory;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance;
......@@ -63,16 +63,17 @@ public class VariableInitializationChecker {
}
}
private final VariableDescriptor myVar;
}
private static class MySemilattice implements Semilattice<Data> {
@NotNull
@Override
public Data initial() {
return new Data(false);
}
private final VariableDescriptor myVar;
}
private static class MySemilattice implements Semilattice<Data> {
@NotNull
@Override
public Data join(@NotNull List<? extends Data> ins) {
......
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.codeInspection.dataflow;
import com.intellij.psi.PsiElement;
......@@ -63,10 +49,4 @@ public class WritesCounterDFAInstance implements DfaInstance<TObjectIntHashMap<G
if (currentVal == 0 || currentVal == 1 && !(variable.getParent() instanceof GrForInClause)) currentVal++;
map.put(variable, currentVal);
}
@NotNull
@Override
public TObjectIntHashMap<GrVariable> initial() {
return new TObjectIntHashMap<>();
}
}
......@@ -23,6 +23,12 @@ public class WritesCounterSemilattice<T> implements Semilattice<TObjectIntHashMa
});
}
@NotNull
@Override
public TObjectIntHashMap<T> initial() {
return new TObjectIntHashMap<>();
}
@NotNull
@Override
public TObjectIntHashMap<T> join(@NotNull List<? extends TObjectIntHashMap<T>> ins) {
......
......@@ -47,7 +47,10 @@ import org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.*;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ControlFlowBuilder;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice;
......@@ -771,6 +774,13 @@ public class ControlFlowUtils {
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
final Semilattice<BitSet> sem = new Semilattice<BitSet>() {
@NotNull
@Override
public BitSet initial() {
return new BitSet(flow.length);
}
@NotNull
@Override
public BitSet join(@NotNull List<? extends BitSet> ins) {
......@@ -803,12 +813,6 @@ public class ControlFlowUtils {
bitSet.clear();
bitSet.set(instruction.num());
}
@NotNull
@Override
public BitSet initial() {
return new BitSet(flow.length);
}
};
return new DFAEngine<>(flow, dfa, sem).performForceDFA();
......
......@@ -19,12 +19,12 @@ import static org.jetbrains.plugins.groovy.lang.psi.controlFlow.OrderUtil.revers
public class DFAEngine<E> {
private final Instruction[] myFlow;
private final DfaInstance<E> myDfa;
private final DfaInstance<? super E> myDfa;
private final Semilattice<E> mySemilattice;
private WorkCounter myCounter = null;
public DFAEngine(@NotNull Instruction[] flow, @NotNull DfaInstance<E> dfa, @NotNull Semilattice<E> semilattice) {
public DFAEngine(@NotNull Instruction[] flow, @NotNull DfaInstance<? super E> dfa, @NotNull Semilattice<E> semilattice) {
myFlow = flow;
myDfa = dfa;
mySemilattice = semilattice;
......@@ -67,7 +67,7 @@ public class DFAEngine<E> {
@Nullable
private List<E> performDFA(boolean timeout) {
final int n = myFlow.length;
final List<E> info = new ArrayList<>(Collections.nCopies(n, myDfa.initial()));
final List<E> info = new ArrayList<>(Collections.nCopies(n, mySemilattice.initial()));
final CallEnvironment env = new MyCallEnvironment(n);
final WorkList workList = new WorkList(n, getFlowOrder());
......
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.dataFlow;
import org.jetbrains.annotations.NotNull;
......@@ -11,9 +11,6 @@ public interface DfaInstance<E> {
void fun(@NotNull E e, @NotNull Instruction instruction);
@NotNull
E initial();
default boolean isForward() {
return true;
}
......
......@@ -7,6 +7,9 @@ import java.util.List;
public interface Semilattice<E> {
@NotNull
E initial();
@NotNull
E join(@NotNull List<? extends E> ins);
......
......@@ -3,9 +3,9 @@ package org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction;
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance;
import java.util.Arrays;
......@@ -35,10 +35,4 @@ public class ReachingDefinitionsDfaInstance implements DfaInstance<DefinitionMap
}
}
}
@Override
@NotNull
public DefinitionMap initial() {
return new DefinitionMap();
}
}
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs;
import org.jetbrains.annotations.NotNull;
......@@ -24,6 +10,13 @@ import java.util.List;
* @author ven
*/
public class ReachingDefinitionsSemilattice implements Semilattice<DefinitionMap> {
@Override
@NotNull
public DefinitionMap initial() {
return new DefinitionMap();
}
@NotNull
@Override
public DefinitionMap join(@NotNull List<? extends DefinitionMap> ins) {
......
......@@ -2,9 +2,9 @@
package org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite
import gnu.trove.TObjectIntHashMap
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction
import org.jetbrains.plugins.groovy.lang.psi.controlFlow.VariableDescriptor
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance
class ReadBeforeWriteInstance(
......@@ -30,7 +30,5 @@ class ReadBeforeWriteInstance(
}
}
override fun initial(): ReadBeforeWriteState = ReadBeforeWriteState.bottom
override fun isReachable(): Boolean = true
}
\ No newline at end of file
}
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice
object ReadBeforeWriteSemilattice : Semilattice<ReadBeforeWriteState> {
private val bottom: ReadBeforeWriteState = ReadBeforeWriteState()
override fun initial(): ReadBeforeWriteState = bottom
override fun join(ins: MutableList<out ReadBeforeWriteState>): ReadBeforeWriteState {
val states = ins.filter { it !== ReadBeforeWriteState.bottom }
val states = ins.filter { it !== bottom }
if (states.isEmpty()) return ReadBeforeWriteState()
val iterator = states.iterator()
val accumulator = iterator.next().clone() // reduce optimized
......@@ -34,7 +24,7 @@ object ReadBeforeWriteSemilattice : Semilattice<ReadBeforeWriteState> {
override fun eq(e1: ReadBeforeWriteState, e2: ReadBeforeWriteState): Boolean {
if (e1 === e2) return true
if (e1 === ReadBeforeWriteState.bottom || e2 === ReadBeforeWriteState.bottom) return e1 === e2
if (e1 === bottom || e2 === bottom) return e1 === e2
return e1.writes == e2.writes && e1.reads == e2.reads
}
}
\ No newline at end of file
}
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite
......@@ -23,11 +9,7 @@ class ReadBeforeWriteState(
val reads: BitSet = BitSet()
) : Cloneable {
companion object {
val bottom: ReadBeforeWriteState = ReadBeforeWriteState()
}
public override fun clone(): ReadBeforeWriteState = ReadBeforeWriteState(writes.clone() as BitSet, reads.clone() as BitSet)
override fun toString(): String = "(writes=$writes, reads=$reads)"
}
\ No newline at end of file
}
......@@ -133,10 +133,4 @@ class TypeDfaInstance implements DfaInstance<TypeDfaState> {
entry.setValue(entry.getValue().negate(negation));
}
}
@Override
@NotNull
public TypeDfaState initial() {
return new TypeDfaState();
}
}
......@@ -25,6 +25,12 @@ public class TypesSemilattice implements Semilattice<TypeDfaState> {
myManager = manager;
}
@Override
@NotNull
public TypeDfaState initial() {
return new TypeDfaState();
}
@NotNull
@Override
public TypeDfaState join(@NotNull List<? extends TypeDfaState> ins) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment