org.checkerframework.dataflow.constantpropagation.ConstantPropagationTransfer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checker Show documentation
Show all versions of checker Show documentation
The Checker Framework enhances Java's type system to
make it more powerful and useful. This lets software developers
detect and prevent errors in their Java programs.
The Checker Framework includes compiler plug-ins ("checkers")
that find bugs or verify their absence. It also permits you to
write your own compiler plug-ins.
package org.checkerframework.dataflow.constantpropagation;
import java.util.List;
import org.checkerframework.dataflow.analysis.ConditionalTransferResult;
import org.checkerframework.dataflow.analysis.ForwardTransferFunction;
import org.checkerframework.dataflow.analysis.RegularTransferResult;
import org.checkerframework.dataflow.analysis.TransferInput;
import org.checkerframework.dataflow.analysis.TransferResult;
import org.checkerframework.dataflow.cfg.UnderlyingAST;
import org.checkerframework.dataflow.cfg.node.AbstractNodeVisitor;
import org.checkerframework.dataflow.cfg.node.AssignmentNode;
import org.checkerframework.dataflow.cfg.node.EqualToNode;
import org.checkerframework.dataflow.cfg.node.IntegerLiteralNode;
import org.checkerframework.dataflow.cfg.node.LocalVariableNode;
import org.checkerframework.dataflow.cfg.node.Node;
public class ConstantPropagationTransfer
extends AbstractNodeVisitor<
TransferResult,
TransferInput>
implements ForwardTransferFunction {
@Override
public ConstantPropagationStore initialStore(
UnderlyingAST underlyingAST, List parameters) {
ConstantPropagationStore store = new ConstantPropagationStore();
return store;
}
@Override
public TransferResult visitLocalVariable(
LocalVariableNode node, TransferInput before) {
ConstantPropagationStore store = before.getRegularStore();
Constant value = store.getInformation(node);
return new RegularTransferResult<>(value, store);
}
@Override
public TransferResult visitNode(
Node n, TransferInput p) {
return new RegularTransferResult<>(null, p.getRegularStore());
}
@Override
public TransferResult visitAssignment(
AssignmentNode n, TransferInput pi) {
ConstantPropagationStore p = pi.getRegularStore();
Node target = n.getTarget();
Constant info = null;
if (target instanceof LocalVariableNode) {
LocalVariableNode t = (LocalVariableNode) target;
info = p.getInformation(n.getExpression());
p.setInformation(t, info);
}
return new RegularTransferResult<>(info, p);
}
@Override
public TransferResult visitIntegerLiteral(
IntegerLiteralNode n, TransferInput pi) {
ConstantPropagationStore p = pi.getRegularStore();
Constant c = new Constant(n.getValue());
p.setInformation(n, c);
return new RegularTransferResult<>(c, p);
}
@Override
public TransferResult visitEqualTo(
EqualToNode n, TransferInput pi) {
ConstantPropagationStore p = pi.getRegularStore();
ConstantPropagationStore old = p.copy();
Node left = n.getLeftOperand();
Node right = n.getRightOperand();
process(p, left, right);
process(p, right, left);
return new ConditionalTransferResult<>(null, p, old);
}
protected void process(ConstantPropagationStore p, Node a, Node b) {
Constant val = p.getInformation(a);
if (b instanceof LocalVariableNode && val.isConstant()) {
p.setInformation(b, val);
}
}
}