KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Synchronized_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>synchronized</code>
11  * block. Contains an expression being tested and a statement to be executed
12  * while the expression is <code>true</code>.
13  */

14 public class Synchronized_c extends Stmt_c implements Synchronized
15 {
16     protected Expr expr;
17     protected Block body;
18
19     public Synchronized_c(Position pos, Expr expr, Block body) {
20     super(pos);
21     this.expr = expr;
22     this.body = body;
23     }
24
25     /** Get the expression to synchronize. */
26     public Expr expr() {
27     return this.expr;
28     }
29
30     /** Set the expression to synchronize. */
31     public Synchronized expr(Expr expr) {
32     Synchronized_c n = (Synchronized_c) copy();
33     n.expr = expr;
34     return n;
35     }
36
37     /** Get the body of the statement. */
38     public Block body() {
39     return this.body;
40     }
41
42     /** Set the body of the statement. */
43     public Synchronized body(Block body) {
44     Synchronized_c n = (Synchronized_c) copy();
45     n.body = body;
46     return n;
47     }
48
49     /** Reconstruct the statement. */
50     protected Synchronized_c reconstruct(Expr expr, Block body) {
51     if (expr != this.expr || body != this.body) {
52         Synchronized_c n = (Synchronized_c) copy();
53         n.expr = expr;
54         n.body = body;
55         return n;
56     }
57
58     return this;
59     }
60
61     /** Visit the children of the statement. */
62     public Node visitChildren(NodeVisitor v) {
63     Expr expr = (Expr) visitChild(this.expr, v);
64     Block body = (Block) visitChild(this.body, v);
65     return reconstruct(expr, body);
66     }
67
68     /** Type check the statement. */
69     public Node typeCheck(TypeChecker tc) throws SemanticException {
70     TypeSystem ts = tc.typeSystem();
71
72     if (! ts.isSubtype(expr.type(), ts.Object()) ) {
73          throw new SemanticException(
74          "Cannot synchronize on an expression of type \"" +
75          expr.type() + "\".", expr.position());
76     }
77
78     return this;
79     }
80
81     public Type childExpectedType(Expr child, AscriptionVisitor av) {
82         TypeSystem ts = av.typeSystem();
83
84         if (child == expr) {
85             return ts.Object();
86         }
87
88         return child.type();
89     }
90
91     public String JavaDoc toString() {
92     return "synchronized (" + expr + ") { ... }";
93     }
94
95     /** Write the statement to an output file. */
96     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
97     w.write("synchronized (");
98     printBlock(expr, w, tr);
99     w.write(") ");
100     printSubStmt(body, w, tr);
101     }
102
103     public Term entry() {
104         return expr.entry();
105     }
106
107     public List acceptCFG(CFGBuilder v, List succs) {
108         v.visitCFG(expr, body.entry());
109         v.visitCFG(body, this);
110         return succs;
111     }
112 }
113
Popular Tags