001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.tree;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.editor.*;
026:
027: /**
028: * An expression in which a definition occurs. Each instance has a unique
029: * version number associated with it.
030: */
031: public abstract class DefExpr extends Expr {
032: Set uses; // Expressions in which the definition is used
033:
034: int version; // Which number DefExpr is this?
035:
036: static int next = 0; // Total number of DefExprs
037:
038: /**
039: * Constructor.
040: *
041: * @param type
042: * The Type (descriptor) of this expression
043: */
044: public DefExpr(final Type type) {
045: super (type);
046: uses = new HashSet();
047: version = DefExpr.next++;
048: }
049:
050: /**
051: * Clean up this expression. Notify all the expressions that use this
052: * definition that it is no longer their defining expression.
053: */
054: public void cleanupOnly() {
055: super .cleanupOnly();
056:
057: final List a = new ArrayList(uses);
058:
059: uses.clear();
060:
061: final Iterator e = a.iterator();
062:
063: while (e.hasNext()) {
064: final Expr use = (Expr) e.next();
065: use.setDef(null);
066: }
067: }
068:
069: /**
070: * Returns Number DefExpr this is. This is also the SSA version number of
071: * the expression that this <tt>DefExpr</tt> defines.
072: */
073: public int version() {
074: return version;
075: }
076:
077: /**
078: * Determines whether or not this <tt>DefExpr</tt> defines a local
079: * variable in its parent.
080: *
081: * @see Assign#defs
082: */
083: public boolean isDef() {
084: if (parent instanceof Assign) {
085: final DefExpr[] defs = ((Assign) parent).defs();
086:
087: if (defs != null) {
088: for (int i = 0; i < defs.length; i++) {
089: if (defs[i] == this ) {
090: return true;
091: }
092: }
093: }
094: }
095:
096: return false;
097: }
098:
099: /**
100: * Returns the <tt>Expr</tt>s in which the variable defined by this are
101: * used.
102: */
103: public Collection uses() {
104: return new HashSet(uses);
105: }
106:
107: public boolean hasUse(final Expr use) {
108: return uses.contains(use);
109: }
110:
111: protected void addUse(final Expr use) {
112: uses.add(use);
113: }
114:
115: protected void removeUse(final Expr use) {
116: uses.remove(use);
117: }
118: }
|