1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.visit.*; 6 import polyglot.util.*; 7 import java.util.*; 8 9 13 public class Case_c extends Stmt_c implements Case 14 { 15 protected Expr expr; 16 protected long value; 17 18 public Case_c(Position pos, Expr expr) { 19 super(pos); 20 this.expr = expr; 21 } 22 23 24 public boolean isDefault() { 25 return this.expr == null; 26 } 27 28 32 public Expr expr() { 33 return this.expr; 34 } 35 36 37 public Case expr(Expr expr) { 38 Case_c n = (Case_c) copy(); 39 n.expr = expr; 40 return n; 41 } 42 43 47 public long value() { 48 return this.value; 49 } 50 51 52 public Case value(long value) { 53 Case_c n = (Case_c) copy(); 54 n.value = value; 55 return n; 56 } 57 58 59 protected Case_c reconstruct(Expr expr) { 60 if (expr != this.expr) { 61 Case_c n = (Case_c) copy(); 62 n.expr = expr; 63 return n; 64 } 65 66 return this; 67 } 68 69 70 public Node visitChildren(NodeVisitor v) { 71 Expr expr = (Expr) visitChild(this.expr, v); 72 return reconstruct(expr); 73 } 74 75 76 public Node typeCheck(TypeChecker tc) throws SemanticException { 77 if (expr == null) { 78 return this; 79 } 80 81 TypeSystem ts = tc.typeSystem(); 82 83 if (! ts.isImplicitCastValid(expr.type(), ts.Int())) { 84 throw new SemanticException( 85 "Case label must be an byte, char, short, or int.", 86 position()); 87 } 88 89 Object o = null; 90 91 if (expr instanceof Field) { 92 FieldInstance fi = ((Field) expr).fieldInstance(); 93 94 if (fi == null) { 95 throw new InternalCompilerError( 96 "Undefined FieldInstance after type-checking."); 97 } 98 99 if (! fi.isConstant()) { 100 throw new SemanticException("Case label must be an integral constant.", 101 position()); 102 } 103 104 o = fi.constantValue(); 105 } 106 else if (expr instanceof Local) { 107 LocalInstance li = ((Local) expr).localInstance(); 108 109 if (li == null) { 110 throw new InternalCompilerError( 111 "Undefined LocalInstance after type-checking."); 112 } 113 114 if (! li.isConstant()) { 115 119 return this; 120 } 121 122 o = li.constantValue(); 123 } 124 else { 125 o = expr.constantValue(); 126 } 127 128 if (o instanceof Number && ! (o instanceof Long ) && 129 ! (o instanceof Float ) && ! (o instanceof Double )) { 130 131 return value(((Number ) o).longValue()); 132 } 133 else if (o instanceof Character ) { 134 return value(((Character ) o).charValue()); 135 } 136 137 throw new SemanticException("Case label must be an integral constant.", 138 position()); 139 } 140 141 public Type childExpectedType(Expr child, AscriptionVisitor av) { 142 TypeSystem ts = av.typeSystem(); 143 144 if (child == expr) { 145 return ts.Int(); 146 } 147 148 return child.type(); 149 } 150 151 public String toString() { 152 if (expr == null) { 153 return "default:"; 154 } 155 else { 156 return "case " + expr + ":"; 157 } 158 } 159 160 161 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 162 if (expr == null) { 163 w.write("default:"); 164 } 165 else { 166 w.write("case "); 167 print(expr, w, tr); 168 w.write(":"); 169 } 170 } 171 172 public Term entry() { 173 if (expr != null) return expr; 174 return this; 175 } 176 177 public List acceptCFG(CFGBuilder v, List succs) { 178 if (expr != null) { 179 v.visitCFG(expr, this); 180 } 181 182 return succs; 183 } 184 } 185 | Popular Tags |