KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Throw_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4
5 import polyglot.util.*;
6 import polyglot.types.*;
7 import polyglot.visit.*;
8 import java.util.*;
9
10 /**
11  * A <code>Throw</code> is an immutable representation of a <code>throw</code>
12  * statement. Such a statement contains a single <code>Expr</code> which
13  * evaluates to the object being thrown.
14  */

15 public class Throw_c extends Stmt_c implements Throw
16 {
17     protected Expr expr;
18
19     public Throw_c(Position pos, Expr expr) {
20     super(pos);
21     this.expr = expr;
22     }
23
24     /** Get the expression to throw. */
25     public Expr expr() {
26     return this.expr;
27     }
28
29     /** Set the expression to throw. */
30     public Throw expr(Expr expr) {
31     Throw_c n = (Throw_c) copy();
32     n.expr = expr;
33     return n;
34     }
35
36     /** Reconstruct the statement. */
37     protected Throw_c reconstruct(Expr expr) {
38     if (expr != this.expr) {
39         Throw_c n = (Throw_c) copy();
40         n.expr = expr;
41         return n;
42     }
43
44     return this;
45     }
46
47     /** Visit the children of the statement. */
48     public Node visitChildren(NodeVisitor v) {
49     Expr expr = (Expr) visitChild(this.expr, v);
50     return reconstruct(expr);
51     }
52
53     /** Type check the statement. */
54     public Node typeCheck(TypeChecker tc) throws SemanticException {
55     if (! expr.type().isThrowable()) {
56         throw new SemanticException(
57         "Can only throw subclasses of \"" +
58         tc.typeSystem().Throwable() + "\".", expr.position());
59     }
60
61     return this;
62     }
63
64     public Type childExpectedType(Expr child, AscriptionVisitor av) {
65         TypeSystem ts = av.typeSystem();
66
67         if (child == expr) {
68             return ts.Throwable();
69         }
70
71         return child.type();
72     }
73
74     public String JavaDoc toString() {
75     return "throw " + expr + ";";
76     }
77
78     /** Write the statement to an output file. */
79     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
80     w.write("throw ");
81     print(expr, w, tr);
82     w.write(";");
83     }
84
85     public Term entry() {
86         return expr.entry();
87     }
88
89     public List acceptCFG(CFGBuilder v, List succs) {
90         v.visitCFG(expr, this);
91
92         // Throw edges will be handled by visitor.
93
return Collections.EMPTY_LIST;
94     }
95
96     public List throwTypes(TypeSystem ts) {
97         // if the exception that a throw statement is given to throw is null,
98
// then a NullPointerException will be thrown.
99
return CollectionUtil.list(expr.type(), ts.NullPointerException());
100     }
101 }
102
Popular Tags