Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ForStatement.java
index e6a5901..7d0f14c 100644 (file)
@@ -1,19 +1,23 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials 
- * are made available under the terms of the Common Public License v0.5 
+ * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v05.html
+ * http://www.eclipse.org/legal/cpl-v10.html
  * 
  * Contributors:
  *     IBM Corporation - initial API and implementation
- ******************************************************************************/
+ *******************************************************************************/
 package net.sourceforge.phpdt.internal.compiler.ast;
 
-import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
-import net.sourceforge.phpdt.internal.compiler.codegen.*;
-import net.sourceforge.phpdt.internal.compiler.flow.*;
-import net.sourceforge.phpdt.internal.compiler.lookup.*;
+import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
+import net.sourceforge.phpdt.internal.compiler.codegen.Label;
+import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
+import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
+import net.sourceforge.phpdt.internal.compiler.flow.LoopingFlowContext;
+import net.sourceforge.phpdt.internal.compiler.impl.Constant;
+import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
 
 public class ForStatement extends Statement {
        
@@ -70,15 +74,18 @@ public class ForStatement extends Statement {
                preCondInitStateIndex =
                        currentScope.methodScope().recordInitializationStates(flowInfo);
 
-               boolean conditionIsInlinedToTrue = 
-                       condition == null || (condition.constant != NotAConstant && condition.constant.booleanValue() == true);
-               boolean conditionIsInlinedToFalse = 
-                       ! conditionIsInlinedToTrue && (condition.constant != NotAConstant && condition.constant.booleanValue() == false);
+               Constant cst = this.condition == null ? null : this.condition.constant;
+               boolean isConditionTrue = cst == null || (cst != NotAConstant && cst.booleanValue() == true);
+               boolean isConditionFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
+
+               cst = this.condition == null ? null : this.condition.optimizedBooleanConstant();
+               boolean isConditionOptimizedTrue = cst == null ||  (cst != NotAConstant && cst.booleanValue() == true);
+               boolean isConditionOptimizedFalse = cst != null && (cst != NotAConstant && cst.booleanValue() == false);
                
                // process the condition
                LoopingFlowContext condLoopContext = null;
                if (condition != null) {
-                       if (!conditionIsInlinedToTrue) {
+                       if (!isConditionTrue) {
                                flowInfo =
                                        condition.analyseCode(
                                                scope,
@@ -91,13 +98,14 @@ public class ForStatement extends Statement {
                // process the action
                LoopingFlowContext loopingContext;
                FlowInfo actionInfo;
-               if ((action == null) || action.isEmptyBlock()) {
+               if (action == null ){
+//                     || (action.isEmptyBlock() && currentScope.environment().options.complianceLevel <= CompilerOptions.JDK1_3)) {
                        if (condLoopContext != null)
                                condLoopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
-                       if (conditionIsInlinedToTrue) {
-                               return FlowInfo.DeadEnd;
+                       if (isConditionTrue) {
+                               return FlowInfo.DEAD_END;
                        } else {
-                               if (conditionIsInlinedToFalse){
+                               if (isConditionFalse){
                                        continueLabel = null; // for(;false;p());
                                }
                                actionInfo = flowInfo.initsWhenTrue().copy();
@@ -111,17 +119,20 @@ public class ForStatement extends Statement {
                        condIfTrueInitStateIndex =
                                currentScope.methodScope().recordInitializationStates(initsWhenTrue);
 
-                               actionInfo = conditionIsInlinedToFalse
-                                       ? FlowInfo.DeadEnd  // unreachable when condition inlined to false
-                                       : initsWhenTrue.copy();
-                       if (!actionInfo.complainIfUnreachable(action, scope)) {
+                               if (isConditionFalse) {
+                                       actionInfo = FlowInfo.DEAD_END;
+                               } else {
+                                       actionInfo = initsWhenTrue.copy();
+                                       if (isConditionOptimizedFalse){
+                                               actionInfo.setReachMode(FlowInfo.UNREACHABLE);
+                                       }
+                               }
+                       if (!actionInfo.complainIfUnreachable(action, scope, false)) {
                                actionInfo = action.analyseCode(scope, loopingContext, actionInfo);
                        }
 
                        // code generation can be optimized when no need to continue in the loop
-                       if (((actionInfo == FlowInfo.DeadEnd) || actionInfo.isFakeReachable())
-                               && ((loopingContext.initsOnContinue == FlowInfo.DeadEnd)
-                                       || loopingContext.initsOnContinue.isFakeReachable())) {
+                       if (!actionInfo.isReachable() && !loopingContext.initsOnContinue.isReachable()) {
                                continueLabel = null;
                        } else {
                                if (condLoopContext != null)
@@ -138,12 +149,12 @@ public class ForStatement extends Statement {
                        int i = 0, count = increments.length;
                        while (i < count)
                                actionInfo = increments[i++].analyseCode(scope, loopContext, actionInfo);
-                       loopContext.complainOnFinalAssignmentsInLoop(scope, flowInfo);
+                       loopContext.complainOnFinalAssignmentsInLoop(scope, actionInfo);
                }
 
                // infinite loop
                FlowInfo mergedInfo;
-               if (conditionIsInlinedToTrue) {
+               if (isConditionOptimizedTrue) {
                        mergedInitStateIndex =
                                currentScope.methodScope().recordInitializationStates(
                                        mergedInfo = loopingContext.initsOnBreak);
@@ -154,6 +165,9 @@ public class ForStatement extends Statement {
                mergedInfo =
                        flowInfo.initsWhenFalse().unconditionalInits().mergedWith(
                                loopingContext.initsOnBreak.unconditionalInits());
+               if (isConditionOptimizedTrue && continueLabel == null){
+                       mergedInfo.setReachMode(FlowInfo.UNREACHABLE);
+               }
                mergedInitStateIndex =
                        currentScope.methodScope().recordInitializationStates(mergedInfo);
                return mergedInfo;
@@ -162,97 +176,131 @@ public class ForStatement extends Statement {
        /**
         * For statement code generation
         *
-        * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
-        * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
+        * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
+        * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
         */
-       public void generateCode(BlockScope currentScope, CodeStream codeStream) {
+//     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
+//
+//             if ((bits & IsReachableMASK) == 0) {
+//                     return;
+//             }
+//             int pc = codeStream.position;
+//
+//             // generate the initializations
+//             if (initializations != null) {
+//                     for (int i = 0, max = initializations.length; i < max; i++) {
+//                             initializations[i].generateCode(scope, codeStream);
+//                     }
+//             }
+//
+//             // label management
+//             Label actionLabel = new Label(codeStream);
+//             Label conditionLabel = new Label(codeStream);
+//             breakLabel.codeStream = codeStream;
+//             if (continueLabel != null) {
+//                     continueLabel.codeStream = codeStream;
+//             }
+//             // jump over the actionBlock
+//             if ((condition != null)
+//                     && (condition.constant == NotAConstant)
+//                     && !((action == null || action.isEmptyBlock()) && (increments == null))) {
+//                     int jumpPC = codeStream.position;
+//                     codeStream.goto_(conditionLabel);
+//                     codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
+//             }
+//             // generate the loop action
+//             actionLabel.place();
+//             if (action != null) {
+//                     // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
+//                     if (condIfTrueInitStateIndex != -1) {
+//                             // insert all locals initialized inside the condition into the action generated prior to the condition
+//                             codeStream.addDefinitelyAssignedVariables(
+//                                     currentScope,
+//                                     condIfTrueInitStateIndex);
+//                     }
+//                     action.generateCode(scope, codeStream);
+//             }
+//             // continuation point
+//             if (continueLabel != null) {
+//                     continueLabel.place();
+//                     // generate the increments for next iteration
+//                     if (increments != null) {
+//                             for (int i = 0, max = increments.length; i < max; i++) {
+//                                     increments[i].generateCode(scope, codeStream);
+//                             }
+//                     }
+//             }
+//
+//             // May loose some local variable initializations : affecting the local variable attributes
+//             if (preCondInitStateIndex != -1) {
+//                     codeStream.removeNotDefinitelyAssignedVariables(
+//                             currentScope,
+//                             preCondInitStateIndex);
+//             }
+//
+//             // generate the condition
+//             conditionLabel.place();
+//             if ((condition != null) && (condition.constant == NotAConstant)) {
+//                     condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
+//             } else {
+//                     if (continueLabel != null) {
+//                             codeStream.goto_(actionLabel);
+//                     }
+//             }
+//             breakLabel.place();
+//
+//             // May loose some local variable initializations : affecting the local variable attributes
+//             if (neededScope) {
+//                     codeStream.exitUserScope(scope);
+//             }
+//             if (mergedInitStateIndex != -1) {
+//                     codeStream.removeNotDefinitelyAssignedVariables(
+//                             currentScope,
+//                             mergedInitStateIndex);
+//             }
+//             codeStream.recordPositionsFrom(pc, this.sourceStart);
+//     }
 
-               if ((bits & IsReachableMASK) == 0) {
-                       return;
+       public void resetStateForCodeGeneration() {
+               if (this.breakLabel != null) {
+                       this.breakLabel.resetStateForCodeGeneration();
                }
-               int pc = codeStream.position;
-
-               // generate the initializations
-               if (initializations != null) {
-                       for (int i = 0, max = initializations.length; i < max; i++) {
-                               initializations[i].generateCode(scope, codeStream);
-                       }
+               if (this.continueLabel != null) {
+                       this.continueLabel.resetStateForCodeGeneration();
                }
+       }
+       public StringBuffer printStatement(int tab, StringBuffer output) {
 
-               // label management
-               Label actionLabel = new Label(codeStream);
-               Label conditionLabel = new Label(codeStream);
-               breakLabel.codeStream = codeStream;
-               if (continueLabel != null) {
-                       continueLabel.codeStream = codeStream;
-               }
-               // jump over the actionBlock
-               if ((condition != null)
-                       && (condition.constant == NotAConstant)
-                       && !((action == null || action.isEmptyBlock()) && (increments == null))) {
-                       int jumpPC = codeStream.position;
-                       codeStream.goto_(conditionLabel);
-                       codeStream.recordPositionsFrom(jumpPC, condition.sourceStart);
-               }
-               // generate the loop action
-               actionLabel.place();
-               if (action != null) {
-                       // Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
-                       if (condIfTrueInitStateIndex != -1) {
-                               // insert all locals initialized inside the condition into the action generated prior to the condition
-                               codeStream.addDefinitelyAssignedVariables(
-                                       currentScope,
-                                       condIfTrueInitStateIndex);
-                       }
-                       action.generateCode(scope, codeStream);
-               }
-               // continuation point
-               if (continueLabel != null) {
-                       continueLabel.place();
-                       // generate the increments for next iteration
-                       if (increments != null) {
-                               for (int i = 0, max = increments.length; i < max; i++) {
-                                       increments[i].generateCode(scope, codeStream);
-                               }
+               printIndent(tab, output).append("for ("); //$NON-NLS-1$
+               //inits
+               if (initializations != null) {
+                       for (int i = 0; i < initializations.length; i++) {
+                               //nice only with expressions
+                               if (i > 0) output.append(", "); //$NON-NLS-1$
+                               initializations[i].print(0, output);
                        }
                }
-
-               // May loose some local variable initializations : affecting the local variable attributes
-               if (preCondInitStateIndex != -1) {
-                       codeStream.removeNotDefinitelyAssignedVariables(
-                               currentScope,
-                               preCondInitStateIndex);
-               }
-
-               // generate the condition
-               conditionLabel.place();
-               if ((condition != null) && (condition.constant == NotAConstant)) {
-                       condition.generateOptimizedBoolean(scope, codeStream, actionLabel, null, true);
-               } else {
-                       if (continueLabel != null) {
-                               codeStream.goto_(actionLabel);
+               output.append("; "); //$NON-NLS-1$
+               //cond
+               if (condition != null) condition.printExpression(0, output);
+               output.append("; "); //$NON-NLS-1$
+               //updates
+               if (increments != null) {
+                       for (int i = 0; i < increments.length; i++) {
+                               if (i > 0) output.append(", "); //$NON-NLS-1$
+                               increments[i].print(0, output);
                        }
                }
-               breakLabel.place();
-
-               // May loose some local variable initializations : affecting the local variable attributes
-               if (neededScope) {
-                       codeStream.exitUserScope(scope);
-               }
-               if (mergedInitStateIndex != -1) {
-                       codeStream.removeNotDefinitelyAssignedVariables(
-                               currentScope,
-                               mergedInitStateIndex);
+               output.append(") "); //$NON-NLS-1$
+               //block
+               if (action == null)
+                       output.append(';');
+               else {
+                       output.append('\n');
+                       action.printStatement(tab + 1, output); //$NON-NLS-1$
                }
-               codeStream.recordPositionsFrom(pc, this.sourceStart);
-       }
-
-       public void resetStateForCodeGeneration() {
-
-               this.breakLabel.resetStateForCodeGeneration();
-               this.continueLabel.resetStateForCodeGeneration();
+               return output.append(';');
        }
-
        public void resolve(BlockScope upperScope) {
 
                // use the scope that will hold the init declarations
@@ -309,7 +357,7 @@ public class ForStatement extends Statement {
        }
        
        public void traverse(
-               IAbstractSyntaxTreeVisitor visitor,
+           ASTVisitor visitor,
                BlockScope blockScope) {
 
                if (visitor.visit(this, blockScope)) {
@@ -333,4 +381,4 @@ public class ForStatement extends Statement {
                }
                visitor.endVisit(this, blockScope);
        }
-}
\ No newline at end of file
+}