1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 5 import polyglot.util.*; 6 import polyglot.visit.*; 7 import polyglot.types.*; 8 import java.util.*; 9 10 15 public class Cast_c extends Expr_c implements Cast 16 { 17 protected TypeNode castType; 18 protected Expr expr; 19 20 public Cast_c(Position pos, TypeNode castType, Expr expr) { 21 super(pos); 22 this.castType = castType; 23 this.expr = expr; 24 } 25 26 27 public Precedence precedence() { 28 return Precedence.CAST; 29 } 30 31 32 public TypeNode castType() { 33 return this.castType; 34 } 35 36 37 public Cast castType(TypeNode castType) { 38 Cast_c n = (Cast_c) copy(); 39 n.castType = castType; 40 return n; 41 } 42 43 44 public Expr expr() { 45 return this.expr; 46 } 47 48 49 public Cast expr(Expr expr) { 50 Cast_c n = (Cast_c) copy(); 51 n.expr = expr; 52 return n; 53 } 54 55 56 protected Cast_c reconstruct(TypeNode castType, Expr expr) { 57 if (castType != this.castType || expr != this.expr) { 58 Cast_c n = (Cast_c) copy(); 59 n.castType = castType; 60 n.expr = expr; 61 return n; 62 } 63 64 return this; 65 } 66 67 68 public Node visitChildren(NodeVisitor v) { 69 TypeNode castType = (TypeNode) visitChild(this.castType, v); 70 Expr expr = (Expr) visitChild(this.expr, v); 71 return reconstruct(castType, expr); 72 } 73 74 75 public Node typeCheck(TypeChecker tc) throws SemanticException 76 { 77 TypeSystem ts = tc.typeSystem(); 78 79 if (! ts.isCastValid(expr.type(), castType.type())) { 80 throw new SemanticException("Cannot cast the expression of type \"" 81 + expr.type() + "\" to type \"" 82 + castType.type() + "\".", 83 position()); 84 } 85 86 return type(castType.type()); 87 } 88 89 public Type childExpectedType(Expr child, AscriptionVisitor av) { 90 TypeSystem ts = av.typeSystem(); 91 92 if (child == expr) { 93 if (castType.type().isReference()) { 94 return ts.Object(); 95 } 96 else if (castType.type().isNumeric()) { 97 return ts.Double(); 98 } 99 else if (castType.type().isBoolean()) { 100 return ts.Boolean(); 101 } 102 } 103 104 return child.type(); 105 } 106 107 public String toString() { 108 return "(" + castType + ") " + expr; 109 } 110 111 112 public void prettyPrint(CodeWriter w, PrettyPrinter tr) 113 { 114 w.begin(0); 115 w.write("("); 116 print(castType, w, tr); 117 w.write(")"); 118 w.allowBreak(2, " "); 119 printSubExpr(expr, w, tr); 120 w.end(); 121 } 122 123 public Term entry() { 124 return expr.entry(); 125 } 126 127 public List acceptCFG(CFGBuilder v, List succs) { 128 v.visitCFG(expr, this); 129 return succs; 130 } 131 132 public List throwTypes(TypeSystem ts) { 133 if (expr.type().isReference()) { 134 return Collections.singletonList(ts.ClassCastException()); 135 } 136 137 return Collections.EMPTY_LIST; 138 } 139 140 public boolean isConstant() { 141 return expr.isConstant() && castType.type().isPrimitive(); 142 } 143 144 public Object constantValue() { 145 Object v = expr.constantValue(); 146 147 if (v == null) { 148 return null; 149 } 150 151 if (v instanceof Boolean ) { 152 if (castType.type().isBoolean()) return v; 153 } 154 155 if (v instanceof String ) { 156 TypeSystem ts = castType.type().typeSystem(); 157 if (castType.type().equals(ts.String())) return v; 158 } 159 160 if (v instanceof Double ) { 161 double vv = ((Double ) v).doubleValue(); 162 163 if (castType.type().isDouble()) return new Double ((double) vv); 164 if (castType.type().isFloat()) return new Float ((float) vv); 165 if (castType.type().isLong()) return new Long ((long) vv); 166 if (castType.type().isInt()) return new Integer ((int) vv); 167 if (castType.type().isChar()) return new Character ((char) vv); 168 if (castType.type().isShort()) return new Short ((short) vv); 169 if (castType.type().isByte()) return new Byte ((byte) vv); 170 } 171 172 if (v instanceof Float ) { 173 float vv = ((Float ) v).floatValue(); 174 175 if (castType.type().isDouble()) return new Double ((double) vv); 176 if (castType.type().isFloat()) return new Float ((float) vv); 177 if (castType.type().isLong()) return new Long ((long) vv); 178 if (castType.type().isInt()) return new Integer ((int) vv); 179 if (castType.type().isChar()) return new Character ((char) vv); 180 if (castType.type().isShort()) return new Short ((short) vv); 181 if (castType.type().isByte()) return new Byte ((byte) vv); 182 } 183 184 if (v instanceof Number ) { 185 long vv = ((Number ) v).longValue(); 186 187 if (castType.type().isDouble()) return new Double ((double) vv); 188 if (castType.type().isFloat()) return new Float ((float) vv); 189 if (castType.type().isLong()) return new Long ((long) vv); 190 if (castType.type().isInt()) return new Integer ((int) vv); 191 if (castType.type().isChar()) return new Character ((char) vv); 192 if (castType.type().isShort()) return new Short ((short) vv); 193 if (castType.type().isByte()) return new Byte ((byte) vv); 194 } 195 196 if (v instanceof Character ) { 197 char vv = ((Character ) v).charValue(); 198 199 if (castType.type().isDouble()) return new Double ((double) vv); 200 if (castType.type().isFloat()) return new Float ((float) vv); 201 if (castType.type().isLong()) return new Long ((long) vv); 202 if (castType.type().isInt()) return new Integer ((int) vv); 203 if (castType.type().isChar()) return new Character ((char) vv); 204 if (castType.type().isShort()) return new Short ((short) vv); 205 if (castType.type().isByte()) return new Byte ((byte) vv); 206 } 207 208 return null; 210 } 211 } 212 | Popular Tags |