KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import java.util.*;
8
9 /**
10  * An <code>Instanceof</code> is an immutable representation of
11  * the use of the <code>instanceof</code> operator.
12  */

13 public class Instanceof_c extends Expr_c implements Instanceof
14 {
15     protected Expr expr;
16     protected TypeNode compareType;
17
18     public Instanceof_c(Position pos, Expr expr, TypeNode compareType) {
19     super(pos);
20     this.expr = expr;
21     this.compareType = compareType;
22     }
23
24     /** Get the precedence of the expression. */
25     public Precedence precedence() {
26     return Precedence.INSTANCEOF;
27     }
28
29     /** Get the expression to be tested. */
30     public Expr expr() {
31     return this.expr;
32     }
33
34     /** Set the expression to be tested. */
35     public Instanceof expr(Expr expr) {
36     Instanceof_c n = (Instanceof_c) copy();
37     n.expr = expr;
38     return n;
39     }
40
41     /** Get the type to be compared against. */
42     public TypeNode compareType() {
43     return this.compareType;
44     }
45
46     /** Set the type to be compared against. */
47     public Instanceof compareType(TypeNode compareType) {
48     Instanceof_c n = (Instanceof_c) copy();
49     n.compareType = compareType;
50     return n;
51     }
52
53     /** Reconstruct the expression. */
54     protected Instanceof_c reconstruct(Expr expr, TypeNode compareType) {
55     if (expr != this.expr || compareType != this.compareType) {
56         Instanceof_c n = (Instanceof_c) copy();
57         n.expr = expr;
58         n.compareType = compareType;
59         return n;
60     }
61
62     return this;
63     }
64
65     /** Visit the children of the expression. */
66     public Node visitChildren(NodeVisitor v) {
67     Expr expr = (Expr) visitChild(this.expr, v) ;
68     TypeNode compareType = (TypeNode) visitChild(this.compareType, v);
69     return reconstruct(expr, compareType);
70     }
71
72     /** Type check the expression. */
73     public Node typeCheck(TypeChecker tc) throws SemanticException {
74         TypeSystem ts = tc.typeSystem();
75
76         if (! compareType.type().isReference()) {
77         throw new SemanticException(
78         "Type operand of \"instanceof\" must be a reference type.",
79         compareType.position());
80     }
81
82     if (! ts.isCastValid(expr.type(), compareType.type())) {
83         throw new SemanticException(
84         "Expression operand incompatible with type in \"instanceof\".",
85         expr.position());
86     }
87
88     return type(ts.Boolean());
89     }
90
91     public Type childExpectedType(Expr child, AscriptionVisitor av) {
92         TypeSystem ts = av.typeSystem();
93
94         if (child == expr) {
95             return ts.Object();
96         }
97
98         return child.type();
99     }
100
101     public String JavaDoc toString() {
102     return expr + " instanceof " + compareType;
103     }
104
105     /** Write the expression to an output file. */
106     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
107     printSubExpr(expr, w, tr);
108     w.write(" instanceof ");
109     print(compareType, w, tr);
110     }
111
112     public Term entry() {
113         return expr.entry();
114     }
115
116     public List acceptCFG(CFGBuilder v, List succs) {
117         v.visitCFG(expr, this);
118         return succs;
119     }
120 }
121
Popular Tags