1 24 25 package org.aspectj.compiler.base.ast; 26 27 import org.aspectj.compiler.base.FlowCheckerPass; 28 import org.aspectj.compiler.base.JavaCompiler; 29 import org.aspectj.compiler.base.CodeWriter; 30 import java.io.IOException ; 31 32 import org.aspectj.compiler.base.bcg.CodeBuilder; 33 import org.aspectj.compiler.base.bcg.Label; 34 35 40 41 public class InstanceofExpr extends Expr { 42 43 public Type discoverType() { 44 return getTypeManager().booleanType; 45 } 46 47 public void unparse(CodeWriter writer) throws IOException { 48 writer.write(expr); 49 writer.write(" instanceof "); 50 writer.write(testTypeD); 51 } 52 53 public boolean isAlwaysFalse() { 54 return !expr.getType().isCoercableTo(testTypeD.getType()); 55 } 56 57 public void checkSpec() { 58 if (!testTypeD.getType().isReferenceType()) { 59 testTypeD.showError("not a reference type"); 60 } 61 62 if (isAlwaysFalse()) { 63 showError("Impossible for " + expr.getType().getString() 64 + " to be instanceof " + testTypeD.getType().getString()); 65 } 66 } 67 68 public ASTObject postMove(MovingWalker walker) { 70 return simplify(); 74 } 75 76 public Expr simplify() { 78 if (expr.getType() == null) return this; 80 81 if (testTypeD.getType().isAssignableFrom(expr.getType())) { 82 return getAST().makeLiteral(true); 83 } else if (isAlwaysFalse()) { 84 return getAST().makeLiteral(false); 85 } else { 86 return this; 87 } 88 } 89 90 protected void cgValue(CodeBuilder cb) { 93 expr.cgValue(cb); 94 testTypeD.getType().emitInstanceof(cb); 95 } 96 97 protected Expr expr; 99 public Expr getExpr() { return expr; } 100 public void setExpr(Expr _expr) { 101 if (_expr != null) _expr.setParent(this); 102 expr = _expr; 103 } 104 105 protected TypeD testTypeD; 106 public TypeD getTestTypeD() { return testTypeD; } 107 public void setTestTypeD(TypeD _testTypeD) { 108 if (_testTypeD != null) _testTypeD.setParent(this); 109 testTypeD = _testTypeD; 110 } 111 112 public InstanceofExpr(SourceLocation location, Expr _expr, TypeD _testTypeD) { 113 super(location); 114 setExpr(_expr); 115 setTestTypeD(_testTypeD); 116 } 117 protected InstanceofExpr(SourceLocation source) { 118 super(source); 119 } 120 121 public ASTObject copyWalk(CopyWalker walker) { 122 InstanceofExpr ret = new InstanceofExpr(getSourceLocation()); 123 ret.preCopy(walker, this); 124 if (expr != null) ret.setExpr( (Expr)walker.process(expr) ); 125 if (testTypeD != null) ret.setTestTypeD( (TypeD)walker.process(testTypeD) ); 126 return ret; 127 } 128 129 public ASTObject getChildAt(int childIndex) { 130 switch(childIndex) { 131 case 0: return expr; 132 case 1: return testTypeD; 133 default: return super.getChildAt(childIndex); 134 } 135 } 136 public String getChildNameAt(int childIndex) { 137 switch(childIndex) { 138 case 0: return "expr"; 139 case 1: return "testTypeD"; 140 default: return super.getChildNameAt(childIndex); 141 } 142 } 143 public void setChildAt(int childIndex, ASTObject child) { 144 switch(childIndex) { 145 case 0: setExpr((Expr)child); return; 146 case 1: setTestTypeD((TypeD)child); return; 147 default: super.setChildAt(childIndex, child); return; 148 } 149 } 150 public int getChildCount() { 151 return 2; 152 } 153 154 public String getDefaultDisplayName() { 155 return "InstanceofExpr()"; 156 } 157 158 } 160 | Popular Tags |