Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / flow / FinallyFlowContext.java
index 61aec21..9adb823 100644 (file)
@@ -1,20 +1,19 @@
 /*******************************************************************************
- * 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.flow;
 
-import net.sourceforge.phpdt.internal.compiler.ast.AstNode;
-import net.sourceforge.phpdt.internal.compiler.ast.NameReference;
+import net.sourceforge.phpdt.internal.compiler.ast.ASTNode;
 import net.sourceforge.phpdt.internal.compiler.ast.Reference;
-import net.sourceforge.phpdt.internal.compiler.lookup.BindingIds;
 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.VariableBinding;
 
@@ -25,9 +24,10 @@ import net.sourceforge.phpdt.internal.compiler.lookup.VariableBinding;
 public class FinallyFlowContext extends FlowContext {
        
        Reference finalAssignments[];
+       VariableBinding finalVariables[];
        int assignCount;
        
-       public FinallyFlowContext(FlowContext parent, AstNode associatedNode) {
+       public FinallyFlowContext(FlowContext parent, ASTNode associatedNode) {
                super(parent, associatedNode);
        }
 
@@ -40,33 +40,46 @@ public class FinallyFlowContext extends FlowContext {
                FlowInfo flowInfo,
                BlockScope scope) {
                for (int i = 0; i < assignCount; i++) {
-                       Reference ref;
-                       if (((ref = finalAssignments[i]).bits & BindingIds.FIELD) != 0) {
+                       VariableBinding variable = finalVariables[i];
+                       if (variable == null) continue;
+                       
+                       boolean complained = false; // remember if have complained on this final assignment
+                       if (variable instanceof FieldBinding) {
                                // final field
-                               if (flowInfo.isPotentiallyAssigned(ref.fieldBinding())) {
-                                       scope.problemReporter().duplicateInitializationOfBlankFinalField(ref.fieldBinding(), ref);
+                               if (flowInfo.isPotentiallyAssigned((FieldBinding)variable)) {
+                                       complained = true;
+                                       scope.problemReporter().duplicateInitializationOfBlankFinalField((FieldBinding)variable, finalAssignments[i]);
                                }
                        } else {
                                // final local variable
-                               if (flowInfo
-                                       .isPotentiallyAssigned((LocalVariableBinding) ((NameReference) ref).binding)) {
+                               if (flowInfo.isPotentiallyAssigned((LocalVariableBinding) variable)) {
+                                       complained = true;
                                        scope.problemReporter().duplicateInitializationOfFinalLocal(
-                                               (LocalVariableBinding) ((NameReference) ref).binding,
-                                               (NameReference) ref);
+                                               (LocalVariableBinding) variable,
+                                               finalAssignments[i]);
                                }
                        }
                        // any reference reported at this level is removed from the parent context 
                        // where it could also be reported again
-                       FlowContext currentContext = parent;
-                       while (currentContext != null) {
-                               if (currentContext.isSubRoutine()) {
-                                       currentContext.removeFinalAssignmentIfAny(ref);
+                       if (complained) {
+                               FlowContext currentContext = parent;
+                               while (currentContext != null) {
+                                       //if (currentContext.isSubRoutine()) {
+                                       currentContext.removeFinalAssignmentIfAny(finalAssignments[i]);
+                                       //}
+                                       currentContext = currentContext.parent;
                                }
-                               currentContext = currentContext.parent;
                        }
                }
        }
 
+       public String individualToString() {
+               
+               StringBuffer buffer = new StringBuffer("Finally flow context"); //$NON-NLS-1$
+               buffer.append("[finalAssignments count -").append(assignCount).append(']'); //$NON-NLS-1$
+               return buffer.toString();
+       }
+       
        public boolean isSubRoutine() {
                return true;
        }
@@ -76,6 +89,7 @@ public class FinallyFlowContext extends FlowContext {
                Reference finalAssignment) {
                if (assignCount == 0) {
                        finalAssignments = new Reference[5];
+                       finalVariables = new VariableBinding[5];
                } else {
                        if (assignCount == finalAssignments.length)
                                System.arraycopy(
@@ -84,8 +98,25 @@ public class FinallyFlowContext extends FlowContext {
                                        (finalAssignments = new Reference[assignCount * 2]),
                                        0,
                                        assignCount);
+                       System.arraycopy(
+                               finalVariables,
+                               0,
+                               (finalVariables = new VariableBinding[assignCount * 2]),
+                               0,
+                               assignCount);
                };
-               finalAssignments[assignCount++] = finalAssignment;
+               finalAssignments[assignCount] = finalAssignment;
+               finalVariables[assignCount++] = binding;
                return true;
        }
-}
\ No newline at end of file
+
+       void removeFinalAssignmentIfAny(Reference reference) {
+               for (int i = 0; i < assignCount; i++) {
+                       if (finalAssignments[i] == reference) {
+                               finalAssignments[i] = null;
+                               finalVariables[i] = null;
+                               return;
+                       }
+               }
+       }
+}