1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.FlowCheckerPass; 28 import org.aspectj.compiler.base.AssignmentCheckerPass; 29 import org.aspectj.compiler.base.JavaCompiler; 30 import org.aspectj.compiler.base.CodeWriter; 31 import org.aspectj.compiler.base.cst.*; 32 33 import org.aspectj.compiler.base.bcg.CodeBuilder; 34 import org.aspectj.compiler.base.bcg.Label; 35 36 41 42 public class CastExpr extends Expr { 43 44 public boolean canBeCopied() { 45 return expr.isUltimatelyLiteral(); 46 } 47 48 public boolean isUltimatelyLiteral() { 49 return expr.isUltimatelyLiteral(); 50 } 51 52 53 public boolean isPossibleCast() { 54 if (expr.getType() == null || typeD.getType() == null) return true; 55 return expr.getType().isCoercableTo(typeD.getType()); 56 } 57 58 71 72 public void checkSpec() { 73 Type exprType = expr.getType(); 74 Type castType = typeD.getType(); 75 setType(castType); 76 if (!isPossibleCast()) { 77 showError("Can't convert from " + exprType.getString() 78 + " to " + castType.getString()); 79 } 80 } 81 82 public Type discoverType() { 83 return typeD.getType(); 84 } 85 86 public void unparse(CodeWriter writer) { 87 writer.write('('); 88 writer.write(typeD); 89 writer.write(')'); 90 91 if (isSynthetic() && !isPossibleCast()) { 92 writer.write("null"); 94 return; 95 } 96 97 if (isSynthetic() && !(expr instanceof ParenExpr)) { 99 writer.write('('); 100 writer.write(expr); 101 writer.write(')'); 102 } else { 103 writer.write(expr); 104 } 105 } 106 107 protected void cgValue(CodeBuilder cb) { 110 expr.cgValue(cb); 111 expr.getType().emitCast(cb, typeD.getType()); 112 } 113 114 117 public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) { 118 checkSpec(); 119 if (expr instanceof LiteralExpr && ! getTypeD().getType().isObject()) { 120 return getTypeD().getType().foldCast((LiteralExpr) expr); 121 } else { 122 return this; 123 } 124 } 125 126 protected TypeD typeD; 128 public TypeD getTypeD() { return typeD; } 129 public void setTypeD(TypeD _typeD) { 130 if (_typeD != null) _typeD.setParent(this); 131 typeD = _typeD; 132 } 133 134 protected Expr expr; 135 public Expr getExpr() { return expr; } 136 public void setExpr(Expr _expr) { 137 if (_expr != null) _expr.setParent(this); 138 expr = _expr; 139 } 140 141 public CastExpr(SourceLocation location, TypeD _typeD, Expr _expr) { 142 super(location); 143 setTypeD(_typeD); 144 setExpr(_expr); 145 } 146 protected CastExpr(SourceLocation source) { 147 super(source); 148 } 149 150 public ASTObject copyWalk(CopyWalker walker) { 151 CastExpr ret = new CastExpr(getSourceLocation()); 152 ret.preCopy(walker, this); 153 if (typeD != null) ret.setTypeD( (TypeD)walker.process(typeD) ); 154 if (expr != null) ret.setExpr( (Expr)walker.process(expr) ); 155 return ret; 156 } 157 158 public ASTObject getChildAt(int childIndex) { 159 switch(childIndex) { 160 case 0: return typeD; 161 case 1: return expr; 162 default: return super.getChildAt(childIndex); 163 } 164 } 165 public String getChildNameAt(int childIndex) { 166 switch(childIndex) { 167 case 0: return "typeD"; 168 case 1: return "expr"; 169 default: return super.getChildNameAt(childIndex); 170 } 171 } 172 public void setChildAt(int childIndex, ASTObject child) { 173 switch(childIndex) { 174 case 0: setTypeD((TypeD)child); return; 175 case 1: setExpr((Expr)child); return; 176 default: super.setChildAt(childIndex, child); return; 177 } 178 } 179 public int getChildCount() { 180 return 2; 181 } 182 183 public String getDefaultDisplayName() { 184 return "CastExpr()"; 185 } 186 187 } 189 | Popular Tags |