1 package polyglot.frontend; 2 3 import polyglot.ast.*; 4 import polyglot.types.*; 5 import polyglot.util.*; 6 import polyglot.frontend.Compiler; 7 8 import java.util.*; 9 10 17 public abstract class Job 18 { 19 20 protected JobExt ext; 21 22 23 protected ExtensionInfo lang; 24 25 26 protected Node ast; 27 28 29 protected ArrayList passes; 30 31 32 protected int nextPass; 33 34 35 protected Pass runningPass; 36 37 38 protected boolean status; 39 40 41 protected Map passMap; 42 43 44 protected int initialErrorCount; 45 46 47 protected boolean reportedErrors; 48 49 public Job(ExtensionInfo lang, JobExt ext, Node ast) { 50 this.lang = lang; 51 this.ext = ext; 52 this.ast = ast; 53 54 this.passes = null; 55 this.passMap = null; 56 this.nextPass = 0; 57 this.runningPass = null; 58 this.status = true; 59 this.initialErrorCount = 0; 60 this.reportedErrors = false; 61 } 62 63 public JobExt ext() { 64 return ext; 65 } 66 67 72 public BarrierPass lastBarrier() { 73 for (int i = nextPass - 1; i >= 0; i--) { 74 Pass pass = (Pass) passes.get(i); 75 76 if (pass instanceof BarrierPass) { 77 return (BarrierPass)pass; 78 } 79 } 80 81 return null; 82 } 83 84 public void setRunningPass(Pass pass) { 85 if (pass != null) { 87 this.initialErrorCount = compiler().errorQueue().errorCount(); 90 } 91 else { 92 int errorCount = compiler().errorQueue().errorCount(); 95 96 if (errorCount > initialErrorCount) { 97 reportedErrors = true; 98 } 99 } 100 101 runningPass = pass; 102 } 103 104 public boolean isRunning() { 105 return runningPass != null; 106 } 107 108 public Pass runningPass() { 109 return runningPass; 110 } 111 112 113 public Node ast() { 114 return ast; 115 } 116 117 118 public void ast(Node ast) { 119 this.ast = ast; 120 } 121 122 123 public boolean reportedErrors() { 124 return reportedErrors; 125 } 126 127 public void dump(CodeWriter cw) { 128 if (ast != null) { 129 ast.dump(cw); 130 } 131 } 132 133 137 public Context context() { 138 return null; 139 } 140 141 148 public abstract SourceJob sourceJob(); 149 150 154 public Source source() { 155 return this.sourceJob().source(); 156 } 157 158 163 public boolean userSpecified() { 164 return this.source().userSpecified(); 165 } 166 167 172 protected abstract List getPasses(); 173 174 179 public final List passes() { 180 if (passes == null) { 181 init(); 182 } 183 184 return passes; 185 } 186 187 private Map passMap() { 188 if (passMap == null) { 189 init(); 190 } 191 return passMap; 192 } 193 194 198 protected void init() { 199 passes = new ArrayList(getPasses()); 200 passMap = new HashMap(); 201 for (int i = 0; i < passes.size(); i++) { 202 Pass pass = (Pass) passes.get(i); 203 passMap.put(pass.id(), new Integer (i)); 204 } 205 } 206 207 210 public boolean completed() { 211 return pendingPasses().isEmpty(); 212 } 213 214 220 public List completedPasses() { 221 return passes().subList(0, nextPass); 222 } 223 224 230 public List pendingPasses() { 231 return passes().subList(nextPass, passes.size()); 232 } 233 234 237 public boolean completed(Pass.ID id) { 238 Integer i = (Integer ) passMap().get(id); 239 return i != null && i.intValue() < nextPass; 240 } 241 242 245 public boolean pending(Pass.ID id) { 246 Integer i = (Integer ) passMap().get(id); 247 return i != null && i.intValue() >= nextPass; 248 } 249 250 253 public Pass passByID(Pass.ID id) { 254 Integer i = (Integer ) passMap().get(id); 255 256 if (i != null) { 257 return (Pass) passes().get(i.intValue()); 258 } 259 260 throw new InternalCompilerError("No pass named \"" + id + "\"."); 261 } 262 263 267 public Pass getPreviousTo(Pass.ID id) { 268 Integer i = (Integer ) passMap().get(id); 269 270 if (i != null) { 271 if (i.intValue() == 0) 272 return null; 273 return (Pass) passes().get(i.intValue() - 1); 274 } 275 276 throw new InternalCompilerError("No pass named \"" + id + "\"."); 277 } 278 279 283 public Pass nextPass() { 284 if (nextPass < passes().size()) { 285 Pass pass = (Pass) passes().get(nextPass); 286 return pass; 287 } 288 else { 289 return null; 290 } 291 } 292 293 294 public boolean status() { 295 return status; 296 } 297 298 306 public void finishPass(Pass p, boolean okay) { 307 List passes = passes(); 308 309 status &= okay; 310 311 for (int i = nextPass; i < passes.size(); i++) { 312 Pass pass = (Pass) passes.get(i); 313 314 if (pass == p) { 315 nextPass = i + 1; 316 return; 317 } 318 } 319 320 throw new InternalCompilerError("Pass " + p + " was not a pending " + 321 "pass."); 322 } 323 324 public ExtensionInfo extensionInfo() { 325 return lang; 326 } 327 328 public Compiler compiler() { 329 return lang.compiler(); 330 } 331 332 348 public Job spawn(Context c, Node ast, Pass.ID begin, Pass.ID end) { 349 return lang.spawnJob(c, ast, this, begin, end); 350 } 351 } 352
| Popular Tags
|