1 package polyglot.ext.jl.ast; 2 3 import polyglot.ast.*; 4 import polyglot.ast.Assert; 5 import polyglot.types.*; 6 import polyglot.visit.*; 7 import polyglot.util.*; 8 import polyglot.main.Options; 9 import java.util.*; 10 11 14 public class Assert_c extends Stmt_c implements Assert 15 { 16 protected Expr cond; 17 protected Expr errorMessage; 18 19 public Assert_c(Position pos, Expr cond, Expr errorMessage) { 20 super(pos); 21 this.cond = cond; 22 this.errorMessage = errorMessage; 23 } 24 25 26 public Expr cond() { 27 return this.cond; 28 } 29 30 31 public Assert cond(Expr cond) { 32 Assert_c n = (Assert_c) copy(); 33 n.cond = cond; 34 return n; 35 } 36 37 38 public Expr errorMessage() { 39 return this.errorMessage; 40 } 41 42 43 public Assert errorMessage(Expr errorMessage) { 44 Assert_c n = (Assert_c) copy(); 45 n.errorMessage = errorMessage; 46 return n; 47 } 48 49 50 protected Assert_c reconstruct(Expr cond, Expr errorMessage) { 51 if (cond != this.cond || errorMessage != this.errorMessage) { 52 Assert_c n = (Assert_c) copy(); 53 n.cond = cond; 54 n.errorMessage = errorMessage; 55 return n; 56 } 57 58 return this; 59 } 60 61 public Node typeCheck(TypeChecker tc) throws SemanticException { 62 TypeSystem ts = tc.typeSystem(); 63 64 if (! Options.global.assertions) { 65 ErrorQueue eq = tc.errorQueue(); 66 eq.enqueue(ErrorInfo.WARNING, 67 "assert statements are disabled. Recompile " + 68 "with -assert and ensure the post compiler supports " + 69 "assert (e.g., -post \"javac -source 1.4\"). " + 70 "Removing the statement and continuing.", 71 cond.position()); 72 } 73 74 if (! ts.equals(cond.type(), ts.Boolean())) { 75 throw new SemanticException("Condition of assert statement " + 76 "must have boolean type.", 77 cond.position()); 78 } 79 80 if (errorMessage != null && ts.equals(errorMessage.type(), ts.Void())) { 81 throw new SemanticException("Error message in assert statement " + 82 "must have a value.", 83 errorMessage.position()); 84 } 85 86 return this; 87 } 88 89 public Type childExpectedType(Expr child, AscriptionVisitor av) { 90 TypeSystem ts = av.typeSystem(); 91 92 if (child == cond) { 93 return ts.Boolean(); 94 } 95 96 101 102 return child.type(); 103 } 104 105 106 public Node visitChildren(NodeVisitor v) { 107 Expr cond = (Expr) visitChild(this.cond, v); 108 Expr errorMessage = (Expr) visitChild(this.errorMessage, v); 109 return reconstruct(cond, errorMessage); 110 } 111 112 public String toString() { 113 return "assert " + cond.toString() + 114 (errorMessage != null 115 ? ": " + errorMessage.toString() : "") + ";"; 116 } 117 118 119 public void prettyPrint(CodeWriter w, PrettyPrinter tr) { 120 w.write("assert "); 121 print(cond, w, tr); 122 123 if (errorMessage != null) { 124 w.write(": "); 125 print(errorMessage, w, tr); 126 } 127 128 w.write(";"); 129 } 130 131 public void translate(CodeWriter w, Translator tr) { 132 if (! Options.global.assertions) { 133 w.write(";"); 134 } 135 else { 136 prettyPrint(w, tr); 137 } 138 } 139 140 public Term entry() { 141 return cond.entry(); 142 } 143 144 public List acceptCFG(CFGBuilder v, List succs) { 145 if (errorMessage != null) { 146 v.visitCFG(cond, errorMessage.entry()); 147 v.visitCFG(errorMessage, this); 148 } 149 else { 150 v.visitCFG(cond, this); 151 } 152 153 return succs; 154 } 155 } 156 | Popular Tags |