1 pnkfelix 1.1.2.1 // TreeVerifyingVisitor.java, created Mon Jan 10 20:40:46 2000 by pnkfelix 2 cananian 1.1.2.5 // Copyright (C) 2000 Felix S. Klock II <pnkfelix@mit.edu> 3 pnkfelix 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 pnkfelix 1.1.2.1 package harpoon.IR.Tree; 5 pnkfelix 1.1.2.1 6 pnkfelix 1.1.2.1 import harpoon.Util.Util; 7 pnkfelix 1.1.2.1 8 pnkfelix 1.1.2.1 import java.util.Iterator; 9 pnkfelix 1.1.2.1 import java.util.HashSet; 10 pnkfelix 1.1.2.1 11 pnkfelix 1.1.2.1 /** 12 pnkfelix 1.1.2.1 * <code>TreeVerifyingVisitor</code> is a generic Tree Visitor for 13 pnkfelix 1.1.2.1 * verifying particular properties about the Tree Intermediate 14 pnkfelix 1.1.2.1 * Representation for a given Tree. Note that in general 15 pnkfelix 1.1.2.1 * <code>TreeVerifyingVisitor</code>s are not meant to be used in 16 pnkfelix 1.1.2.1 * algorithm implementations or even in general assertions, but rather 17 pnkfelix 1.1.2.1 * to debug particular errors in Tree construction. 18 pnkfelix 1.1.2.1 * 19 pnkfelix 1.1.2.1 * In general, a Verifier for a given invariant will be written to 20 pnkfelix 1.1.2.1 * analyze a given Tree form and assert false when it finds a 21 pnkfelix 1.1.2.1 * violation of the invariant. 22 pnkfelix 1.1.2.1 * 23 cananian 1.1.2.5 * @author Felix S. Klock II <pnkfelix@mit.edu> 24 cananian 1.4 * @version $Id: TreeVerifyingVisitor.java,v 1.4 2002/04/10 03:05:46 cananian Exp $ 25 pnkfelix 1.1.2.1 */ 26 pnkfelix 1.1.2.1 public abstract class TreeVerifyingVisitor extends TreeVisitor { 27 pnkfelix 1.1.2.1 28 pnkfelix 1.1.2.1 /** Creates a <code>TreeVerifyingVisitor</code>. */ 29 pnkfelix 1.1.2.1 public TreeVerifyingVisitor() { } 30 pnkfelix 1.1.2.1 31 pnkfelix 1.1.2.1 32 pnkfelix 1.1.2.1 33 pnkfelix 1.1.2.1 public static TreeVerifyingVisitor norepeats() { 34 pnkfelix 1.1.2.1 return new NoRepeats(); 35 pnkfelix 1.1.2.1 } 36 pnkfelix 1.1.2.1 37 pnkfelix 1.1.2.3 public static final boolean DEBUG = true; 38 pnkfelix 1.1.2.1 39 pnkfelix 1.1.2.1 public static class NoRepeats extends TreeVerifyingVisitor { 40 pnkfelix 1.1.2.1 HashSet haveSeen = new HashSet(); 41 pnkfelix 1.1.2.1 42 pnkfelix 1.1.2.1 boolean firstCall = true; 43 pnkfelix 1.1.2.1 Tree saw = null; 44 pnkfelix 1.1.2.1 45 pnkfelix 1.1.2.1 public void visit(Tree e) { 46 pnkfelix 1.1.2.1 if (!DEBUG) return; 47 pnkfelix 1.1.2.1 48 pnkfelix 1.1.2.1 boolean isFirstCall = firstCall; 49 pnkfelix 1.1.2.1 50 pnkfelix 1.1.2.1 if (e.kids() == null) return; 51 pnkfelix 1.1.2.1 52 pnkfelix 1.1.2.1 haveSeen.add(e); 53 pnkfelix 1.1.2.1 54 pnkfelix 1.1.2.1 Iterator iter = ExpList.iterator(e.kids()); 55 pnkfelix 1.1.2.1 while (iter.hasNext() && (saw == null)) { 56 pnkfelix 1.1.2.1 Tree o =(Tree) iter.next(); 57 cananian 1.3.2.1 assert o != null; 58 pnkfelix 1.1.2.1 if (haveSeen.contains(o)) { 59 pnkfelix 1.1.2.1 saw = o; 60 pnkfelix 1.1.2.1 break; 61 pnkfelix 1.1.2.1 } 62 pnkfelix 1.1.2.1 o.accept(this); 63 pnkfelix 1.1.2.1 } 64 pnkfelix 1.1.2.1 65 cananian 1.3.2.1 assert saw == null : ("should not have seen: "+saw+" in " + e); 66 pnkfelix 1.1.2.1 } 67 pnkfelix 1.1.2.1 68 pnkfelix 1.1.2.1 public void visit(SEQ s) { 69 pnkfelix 1.1.2.3 if(!DEBUG) return; 70 pnkfelix 1.1.2.1 boolean isFirstCall = firstCall; 71 pnkfelix 1.1.2.1 if (isFirstCall) firstCall = false; 72 pnkfelix 1.1.2.1 if (s!= null && haveSeen.contains(s)) saw = s; 73 pnkfelix 1.1.2.1 74 pnkfelix 1.1.2.1 haveSeen.add(s); 75 pnkfelix 1.1.2.1 76 pnkfelix 1.1.2.1 s.getLeft().accept(this); 77 pnkfelix 1.1.2.1 s.getRight().accept(this); 78 pnkfelix 1.1.2.1 79 pnkfelix 1.1.2.1 if (isFirstCall) 80 cananian 1.3.2.1 assert saw == null : ("should not have seen: "+saw+ " in "+s); 81 pnkfelix 1.1.2.1 } 82 pnkfelix 1.1.2.1 public void visit(ESEQ s) { 83 pnkfelix 1.1.2.3 if(!DEBUG) return; 84 pnkfelix 1.1.2.1 boolean isFirstCall = firstCall; 85 pnkfelix 1.1.2.1 if (isFirstCall) firstCall = false; 86 pnkfelix 1.1.2.1 if(s != null && haveSeen.contains(s)) saw = s; 87 pnkfelix 1.1.2.1 88 pnkfelix 1.1.2.1 haveSeen.add(s); 89 pnkfelix 1.1.2.1 90 pnkfelix 1.1.2.1 s.getExp().accept(this); 91 pnkfelix 1.1.2.1 s.getStm().accept(this); 92 pnkfelix 1.1.2.1 93 pnkfelix 1.1.2.1 if (isFirstCall) 94 cananian 1.3.2.1 assert saw == null : ("should not have seen: "+saw+ " in "+s); 95 pnkfelix 1.1.2.1 } 96 pnkfelix 1.1.2.1 } 97 cananian 1.2 }