1 package polyglot.visit; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import polyglot.ast.Node; 7 import polyglot.ast.NodeFactory; 8 import polyglot.frontend.Job; 9 import polyglot.types.SemanticException; 10 import polyglot.types.Type; 11 import polyglot.types.TypeSystem; 12 import polyglot.util.ErrorQueue; 13 import polyglot.util.InternalCompilerError; 14 import polyglot.util.Position; 15 import polyglot.util.SubtypeSet; 16 17 18 public class ExceptionChecker extends ErrorHandlingVisitor 19 { 20 protected ExceptionChecker outer; 21 24 private SubtypeSet scope = null; 25 protected Map exceptionPositions; 26 27 public ExceptionChecker(Job job, TypeSystem ts, NodeFactory nf) { 28 super(job, ts, nf); 29 this.outer = null; 30 this.exceptionPositions = new HashMap (); 31 } 32 33 public ExceptionChecker push() { 34 ExceptionChecker ec = (ExceptionChecker) this.visitChildren(); 35 ec.outer = this; 36 ec.exceptionPositions = new HashMap (); 37 return ec; 38 } 39 40 public ExceptionChecker pop() { 41 return outer; 42 } 43 44 56 protected NodeVisitor enterCall(Node n) throws SemanticException { 57 return n.exceptionCheckEnter(push()); 58 } 59 60 protected NodeVisitor enterError(Node n) { 61 return push(); 62 } 63 64 74 protected Node leaveCall(Node old, Node n, NodeVisitor v) 75 throws SemanticException { 76 77 ExceptionChecker inner = (ExceptionChecker) v; 78 79 if (inner.outer != this) throw new InternalCompilerError("oops!"); 80 81 n = n.del().exceptionCheck(inner); 83 84 SubtypeSet t = inner.throwsSet(); 87 throwsSet().addAll(t); 88 exceptionPositions.putAll(inner.exceptionPositions); 89 90 return n; 91 } 92 93 101 public void throwsException(Type t, Position pos) { 102 throwsSet().add(t) ; 103 exceptionPositions.put(t, pos); 104 } 105 106 110 public SubtypeSet throwsSet() { 111 if (scope == null) { 112 this.scope = new SubtypeSet(ts.Throwable()); 113 } 114 return scope; 115 } 116 117 121 public Position exceptionPosition(Type t) { 122 return (Position)exceptionPositions.get(t); 123 } 124 } 125 | Popular Tags |