KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > pao > extension > PaoBinaryExt_c


1 package polyglot.ext.pao.extension;
2
3 import polyglot.ast.*;
4 import polyglot.ext.pao.types.PaoTypeSystem;
5 import polyglot.types.MethodInstance;
6
7 /**
8  * The <code>PaoExt</code> implementation for the
9  * <code>Binary</code> AST node.
10  */

11 public class PaoBinaryExt_c extends PaoExt_c {
12     /**
13      * Rewrite the binary operators <code>==</code> and <code>&excl;=</code> to
14      * invoke <code>Primitive.equals(o, p)</code>.
15      *
16      * @see PaoExt#rewrite(PaoTypeSystem, NodeFactory)
17      * @see polyglot.ext.pao.runtime.Primitive#equals(Object, Object)
18      */

19     public Node rewrite(PaoTypeSystem ts, NodeFactory nf) {
20         Binary b = (Binary) node();
21         Expr l = b.left();
22         Expr r = b.right();
23
24         if (b.operator() == Binary.EQ || b.operator() == Binary.NE) {
25             MethodInstance mi = ((PaoTypeSystem) ts).primitiveEquals();
26
27             // The container of mi, mi.container(), is the super class of
28
// the runtime boxed representations of primitive values.
29
if (ts.isSubtype(l.type(), mi.container()) ||
30                 ts.equals(l.type(), ts.Object())) {
31                 // The left operand is either a subtype of
32
// polyglot.ext.pao.runtime.Primitive, or it is an
33
// Object, and thus possibly a subtype of
34
// polyglot.ext.pao.runtime.Primitive. Either way,
35
// it may be a boxed primitive.
36
if (r.type().isReference()) {
37                     // The right operand is a reference type, so replace the
38
// binary operation with a call to
39
// Primitive.equals(Object, Object).
40
TypeNode x = nf.CanonicalTypeNode(b.position(),
41                                                       mi.container());
42                     Call y = nf.Call(b.position(), x, mi.name(), l, r);
43                     y = (Call) y.type(mi.returnType());
44                     if (b.operator() == Binary.NE) {
45                         return nf.Unary(b.position(), Unary.NOT, y).type(mi.returnType());
46                     }
47                     else {
48                         return y;
49                     }
50                 }
51             }
52         }
53         
54         // we do not need to rewrite the binary operator.
55
return super.rewrite(ts, nf);
56     }
57 }
58
Popular Tags