1 package polyglot.ext.pao.extension; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.List ; 6 7 import polyglot.ast.*; 8 import polyglot.ext.pao.types.PaoTypeSystem; 9 import polyglot.types.ConstructorInstance; 10 import polyglot.types.MethodInstance; 11 import polyglot.types.Type; 12 13 17 public class PaoCastExt_c extends PaoExt_c { 18 24 public Node rewrite(PaoTypeSystem ts, NodeFactory nf) { 25 26 Cast c = (Cast) node(); 27 Type rtype = c.expr().type(); 28 Type ltype = c.castType().type(); 29 30 if (ltype.isPrimitive() && rtype.isReference()) { 31 35 38 MethodInstance mi = ts.getter(ltype.toPrimitive()); 39 40 Cast x = nf.Cast(c.position(), 41 nf.CanonicalTypeNode(c.position(), mi.container()), 42 c.expr()); 43 x = (Cast) x.type(mi.container()); 44 45 Call y = nf.Call(c.position(), x, mi.name(), 46 Collections.EMPTY_LIST); 47 y = (Call) y.type(mi.returnType()); 48 49 return y.methodInstance(mi); 50 } 51 else if (ltype.isReference() && rtype.isPrimitive()) { 52 56 59 ConstructorInstance ci = ts.wrapper(rtype.toPrimitive()); 60 61 List args = new ArrayList (1); 62 args.add(c.expr()); 63 64 New x = nf.New(c.position(), 65 nf.CanonicalTypeNode(c.position(), ci.container()), 66 args); 67 x = (New) x.type(ci.container()); 68 return x.constructorInstance(ci); 69 } 70 71 return super.rewrite(ts, nf); 73 } 74 } 75 | Popular Tags |