Parses a statement if ( expression ) statement [else statement] @return a Node, never null
()
| 352 | * @return a {@link Node}, never {@code null} |
| 353 | */ |
| 354 | private Node parseIf() { |
| 355 | require("("); |
| 356 | // Parse predicate |
| 357 | var pred = require(parseExpression(), ")").keep(); |
| 358 | // IfNode takes current control and predicate |
| 359 | Node ifNode = new IfNode(ctrl(), pred).peephole(); |
| 360 | // Setup projection nodes |
| 361 | Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep(); |
| 362 | Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole().keep(); |
| 363 | // In if true branch, the ifT proj node becomes the ctrl |
| 364 | // But first clone the scope and set it as current |
| 365 | int ndefs = _scope.nIns(); |
| 366 | ScopeNode fScope = _scope.dup(); // Duplicate current scope |
| 367 | _xScopes.push(fScope); // For graph visualization we need all scopes |
| 368 | |
| 369 | // Parse the true side |
| 370 | ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection |
| 371 | _scope.upcast(ifT,pred,false); // Up-cast predicate |
| 372 | parseStatement(); // Parse true-side |
| 373 | ScopeNode tScope = _scope; |
| 374 | |
| 375 | // Parse the false side |
| 376 | _scope = fScope; // Restore scope, then parse else block if any |
| 377 | ctrl(ifF.unkeep()); // Ctrl token is now set to ifFalse projection |
| 378 | _scope.upcast(ifF,pred,true); // Up-cast predicate |
| 379 | if (matchx("else")) { |
| 380 | parseStatement(); |
| 381 | fScope = _scope; |
| 382 | } |
| 383 | pred.unkeep(); |
| 384 | |
| 385 | if( tScope.nIns() != ndefs || fScope.nIns() != ndefs ) |
| 386 | throw error("Cannot define a new name on one arm of an if"); |
| 387 | |
| 388 | // Merge results |
| 389 | _scope = tScope; |
| 390 | _xScopes.pop(); // Discard pushed from graph display |
| 391 | |
| 392 | return ctrl(tScope.mergeScopes(fScope)); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** |
no test coverage detected