KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > If_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 immutable representation of a Java language <code>if</code> statement.
11  * Contains an expression whose value is tested, a ``then'' statement
12  * (consequent), and optionally an ``else'' statement (alternate).
13  */

14 public class If_c extends Stmt_c implements If
15 {
16     protected Expr cond;
17     protected Stmt consequent;
18     protected Stmt alternative;
19
20     public If_c(Position pos, Expr cond, Stmt consequent, Stmt alternative) {
21     super(pos);
22     this.cond = cond;
23     this.consequent = consequent;
24     this.alternative = alternative;
25     }
26
27     /** Get the conditional of the statement. */
28     public Expr cond() {
29     return this.cond;
30     }
31
32     /** Set the conditional of the statement. */
33     public If cond(Expr cond) {
34     If_c n = (If_c) copy();
35     n.cond = cond;
36     return n;
37     }
38
39     /** Get the consequent of the statement. */
40     public Stmt consequent() {
41     return this.consequent;
42     }
43
44     /** Set the consequent of the statement. */
45     public If consequent(Stmt consequent) {
46     If_c n = (If_c) copy();
47     n.consequent = consequent;
48     return n;
49     }
50
51     /** Get the alternative of the statement. */
52     public Stmt alternative() {
53     return this.alternative;
54     }
55
56     /** Set the alternative of the statement. */
57     public If alternative(Stmt alternative) {
58     If_c n = (If_c) copy();
59     n.alternative = alternative;
60     return n;
61     }
62
63     /** Reconstruct the statement. */
64     protected If_c reconstruct(Expr cond, Stmt consequent, Stmt alternative) {
65     if (cond != this.cond || consequent != this.consequent || alternative != this.alternative) {
66         If_c n = (If_c) copy();
67         n.cond = cond;
68         n.consequent = consequent;
69         n.alternative = alternative;
70         return n;
71     }
72
73     return this;
74     }
75
76     /** Visit the children of the statement. */
77     public Node visitChildren(NodeVisitor v) {
78     Expr cond = (Expr) visitChild(this.cond, v);
79     Stmt consequent = (Stmt) visitChild(this.consequent, v);
80     Stmt alternative = (Stmt) visitChild(this.alternative, v);
81     return reconstruct(cond, consequent, alternative);
82     }
83
84     /** Type check the statement. */
85     public Node typeCheck(TypeChecker tc) throws SemanticException {
86         TypeSystem ts = tc.typeSystem();
87
88     if (! ts.equals(cond.type(), ts.Boolean())) {
89         throw new SemanticException(
90         "Condition of if statement must have boolean type.",
91         cond.position());
92     }
93
94     return this;
95     }
96
97     public Type childExpectedType(Expr child, AscriptionVisitor av) {
98         TypeSystem ts = av.typeSystem();
99
100         if (child == cond) {
101             return ts.Boolean();
102         }
103
104         return child.type();
105     }
106
107     public String JavaDoc toString() {
108     return "if (" + cond + ") " + consequent +
109         (alternative != null ? " else " + alternative : "");
110     }
111
112     /** Write the statement to an output file. */
113     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
114     w.write("if (");
115     printBlock(cond, w, tr);
116     w.write(")");
117        
118     printSubStmt(consequent, w, tr);
119
120     if (alternative != null) {
121         if (consequent instanceof Block) {
122         // allow the "} else {" formatting
123
w.write(" ");
124         }
125         else {
126         w.allowBreak(0, " ");
127         }
128
129         w.write ("else");
130         printSubStmt(alternative, w, tr);
131     }
132     }
133
134     public Term entry() {
135         return cond.entry();
136     }
137
138     public List acceptCFG(CFGBuilder v, List succs) {
139         if (alternative == null) {
140             v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, consequent.entry(),
141                              FlowGraph.EDGE_KEY_FALSE, this);
142             v.visitCFG(consequent, this);
143         }
144         else {
145             v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, consequent.entry(),
146                              FlowGraph.EDGE_KEY_FALSE, alternative.entry());
147             v.visitCFG(consequent, this);
148             v.visitCFG(alternative, this);
149         }
150
151         return succs;
152     }
153 }
154
Popular Tags