Node tryFoldFor ( Node n ) { checkArgument ( n . isVanillaFor ( ) ) ; Node init = n . getFirstChild ( ) ; Node cond = init . getNext ( ) ; Node increment = cond . getNext ( ) ; if ( ! init . isEmpty ( ) && ! NodeUtil . isNameDeclaration ( init ) ) { init = trySimplifyUnusedResult ( init ) ; if ( init == null ) { init = IR . empty ( ) . srcref ( n ) ; n . addChildToFront ( init ) ; } } if ( ! increment . isEmpty ( ) ) { increment = trySimplifyUnusedResult ( increment ) ; if ( increment == null ) { increment = IR . empty ( ) . srcref ( n ) ; n . addChildAfter ( increment , cond ) ; } } // There is an initializer skip it if ( ! n . getFirstChild ( ) . isEmpty ( ) ) { return n ; } if ( NodeUtil . getImpureBooleanValue ( cond ) != TernaryValue . FALSE ) { return n ; } Node parent = n . getParent ( ) ; NodeUtil . redeclareVarsInsideBranch ( n ) ; if ( ! mayHaveSideEffects ( cond ) ) { NodeUtil . removeChild ( parent , n ) ; } else { Node statement = IR . exprResult ( cond . detach ( ) ) . useSourceInfoIfMissingFrom ( cond ) ; if ( parent . isLabel ( ) ) { Node block = IR . block ( ) ; block . useSourceInfoIfMissingFrom ( statement ) ; block . addChildToFront ( statement ) ; statement = block ; } parent . replaceChild ( n , statement ) ; } reportChangeToEnclosingScope ( parent ) ; return null ; }
Removes FORs that always evaluate to false .