All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.checkerframework.dataflow.constantpropagation.ConstantPropagationTransfer Maven / Gradle / Ivy

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 lvn = (LocalVariableNode) target;
      info = p.getInformation(n.getExpression());
      p.setInformation(lvn, 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);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy