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

soot.jimple.toolkits.scalar.ToppedSet Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
package soot.jimple.toolkits.scalar;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 1997 - 1999 Raja Vallee-Rai
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Iterator;
import java.util.List;

import soot.toolkits.scalar.AbstractFlowSet;
import soot.toolkits.scalar.BoundedFlowSet;
import soot.toolkits.scalar.FlowSet;

/**
 * Represents information for flow analysis, adding a top element to a lattice. A FlowSet is an element of a lattice; this
 * lattice might be described by a FlowUniverse. If add, remove, size, isEmpty, toList and contains are implemented, the
 * lattice must be the powerset of some set.
 *
 */
public class ToppedSet extends AbstractFlowSet {
  FlowSet underlyingSet;
  boolean isTop;

  public void setTop(boolean top) {
    isTop = top;
  }

  public boolean isTop() {
    return isTop;
  }

  public ToppedSet(FlowSet under) {
    underlyingSet = under;
  }

  public ToppedSet clone() {
    ToppedSet newSet = new ToppedSet(underlyingSet.clone());
    newSet.setTop(isTop());
    return newSet;
  }

  public void copy(FlowSet d) {
    if (this == d) {
      return;
    }

    ToppedSet dest = (ToppedSet) d;
    dest.isTop = isTop;
    if (!isTop) {
      underlyingSet.copy(dest.underlyingSet);
    }
  }

  public FlowSet emptySet() {
    return new ToppedSet(underlyingSet.emptySet());
  }

  public void clear() {
    isTop = false;
    underlyingSet.clear();
  }

  public void union(FlowSet o, FlowSet d) {
    if (o instanceof ToppedSet && d instanceof ToppedSet) {
      ToppedSet other = (ToppedSet) o;
      ToppedSet dest = (ToppedSet) d;

      if (isTop()) {
        copy(dest);
        return;
      }

      if (other.isTop()) {
        other.copy(dest);
      } else {
        underlyingSet.union(other.underlyingSet, dest.underlyingSet);
        dest.setTop(false);
      }
    } else {
      super.union(o, d);
    }
  }

  public void intersection(FlowSet o, FlowSet d) {
    if (isTop()) {
      o.copy(d);
      return;
    }

    ToppedSet other = (ToppedSet) o, dest = (ToppedSet) d;

    if (other.isTop()) {
      copy(dest);
      return;
    } else {
      underlyingSet.intersection(other.underlyingSet, dest.underlyingSet);
      dest.setTop(false);
    }
  }

  public void difference(FlowSet o, FlowSet d) {
    ToppedSet other = (ToppedSet) o, dest = (ToppedSet) d;

    if (isTop()) {
      if (other.isTop()) {
        dest.clear();
      } else if (other.underlyingSet instanceof BoundedFlowSet) {
        ((BoundedFlowSet) other.underlyingSet).complement(dest);
      } else {
        throw new RuntimeException("can't take difference!");
      }
    } else {
      if (other.isTop()) {
        dest.clear();
      } else {
        underlyingSet.difference(other.underlyingSet, dest.underlyingSet);
      }
    }
  }

  public boolean isEmpty() {
    if (isTop()) {
      return false;
    }
    return underlyingSet.isEmpty();
  }

  public int size() {
    if (isTop()) {
      throw new UnsupportedOperationException();
    }
    return underlyingSet.size();
  }

  public void add(T obj) {
    if (isTop()) {
      return;
    }
    underlyingSet.add(obj);
  }

  public void remove(T obj) {
    if (isTop()) {
      return;
    }
    underlyingSet.remove(obj);
  }

  public boolean contains(T obj) {
    if (isTop()) {
      return true;
    }
    return underlyingSet.contains(obj);
  }

  public List toList() {
    if (isTop()) {
      throw new UnsupportedOperationException();
    }
    return underlyingSet.toList();
  }

  public boolean equals(Object o) {
    if (!(o instanceof ToppedSet)) {
      return false;
    }

    @SuppressWarnings("unchecked")
    ToppedSet other = (ToppedSet) o;
    if (other.isTop() != isTop()) {
      return false;
    }
    return underlyingSet.equals(other.underlyingSet);
  }

  public String toString() {
    if (isTop()) {
      return "{TOP}";
    } else {
      return underlyingSet.toString();
    }
  }

  @Override
  public Iterator iterator() {
    if (isTop()) {
      throw new UnsupportedOperationException();
    }
    return underlyingSet.iterator();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy