improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / flow / ExceptionHandlingFlowContext.java
index 2ee8ae7..9348747 100644 (file)
@@ -1,24 +1,27 @@
 /*******************************************************************************
- * 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 java.util.ArrayList;
 
-import net.sourceforge.phpdt.internal.compiler.ast.AstNode;
-import net.sourceforge.phpdt.internal.compiler.ast.TryStatement;
 import net.sourceforge.phpdt.internal.compiler.codegen.ObjectCache;
 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.CompilerModifiers;
+import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
+import net.sourceforge.phpeclipse.internal.compiler.ast.ASTNode;
+import net.sourceforge.phpeclipse.internal.compiler.ast.AbstractMethodDeclaration;
+import net.sourceforge.phpeclipse.internal.compiler.ast.TryStatement;
 
 /**
  * Reflects the context of code analysis, keeping track of enclosing
@@ -26,7 +29,7 @@ import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
  */
 public class ExceptionHandlingFlowContext extends FlowContext {
        
-       ReferenceBinding[] handledExceptions;
+       public ReferenceBinding[] handledExceptions;
        
        public final static int BitCacheSize = 32; // 32 bits per int
        int[] isReached;
@@ -42,7 +45,7 @@ public class ExceptionHandlingFlowContext extends FlowContext {
        
        public ExceptionHandlingFlowContext(
                FlowContext parent,
-               AstNode associatedNode,
+               ASTNode associatedNode,
                ReferenceBinding[] handledExceptions,
                BlockScope scope,
                UnconditionalFlowInfo flowInfo) {
@@ -63,37 +66,38 @@ public class ExceptionHandlingFlowContext extends FlowContext {
                                isReached[cacheIndex] |= bitMask;
                                this.initsOnExceptions[i] = flowInfo.copy().unconditionalInits();
                        } else {
-                               this.initsOnExceptions[i] = FlowInfo.DeadEnd;
+                               this.initsOnExceptions[i] = FlowInfo.DEAD_END;
                        }
                }
                System.arraycopy(this.isReached, 0, this.isNeeded, 0, cacheSize);
-               this.initsOnReturn = FlowInfo.DeadEnd;  
-       }
-
-       public void complainIfUnusedExceptionHandlers(
-               AstNode[] exceptionHandlers,
-               BlockScope scope,
-               TryStatement tryStatement) {
-               // report errors for unreachable exception handlers
-               for (int i = 0, count = handledExceptions.length; i < count; i++) {
-                       int index = indexes.get(handledExceptions[i]);
-                       int cacheIndex = index / BitCacheSize;
-                       int bitMask = 1 << (index % BitCacheSize);
-                       if ((isReached[cacheIndex] & bitMask) == 0) {
-                               scope.problemReporter().unreachableExceptionHandler(
-                                       handledExceptions[index],
-                                       exceptionHandlers[index]);
-                       } else {
-                               if ((isNeeded[cacheIndex] & bitMask) == 0) {
-                                       scope.problemReporter().maskedExceptionHandler(
-                                               handledExceptions[index],
-                                               exceptionHandlers[index]);
-                               }
-                       }
-               }
-               // will optimized out unnecessary catch block during code gen
-               tryStatement.preserveExceptionHandler = isNeeded;
+               this.initsOnReturn = FlowInfo.DEAD_END; 
        }
+       
+       
+//     public void complainIfUnusedExceptionHandlers(
+//             ASTNode[] exceptionHandlers,
+//             BlockScope scope,
+//             TryStatement tryStatement) {
+//             // report errors for unreachable exception handlers
+//             for (int i = 0, count = handledExceptions.length; i < count; i++) {
+//                     int index = indexes.get(handledExceptions[i]);
+//                     int cacheIndex = index / BitCacheSize;
+//                     int bitMask = 1 << (index % BitCacheSize);
+//                     if ((isReached[cacheIndex] & bitMask) == 0) {
+//                             scope.problemReporter().unreachableExceptionHandler(
+//                                     handledExceptions[index],
+//                                     exceptionHandlers[index]);
+//                     } else {
+//                             if ((isNeeded[cacheIndex] & bitMask) == 0) {
+//                                     scope.problemReporter().maskedExceptionHandler(
+//                                             handledExceptions[index],
+//                                             exceptionHandlers[index]);
+//                             }
+//                     }
+//             }
+//             // will optimized out unnecessary catch block during code gen
+//             tryStatement.preserveExceptionHandler = isNeeded;
+//     }
 
        public String individualToString() {
                
@@ -114,6 +118,7 @@ public class ExceptionHandlingFlowContext extends FlowContext {
                        }
                        buffer.append('-').append(initsOnExceptions[i].toString()).append(']');
                }
+               buffer.append("[initsOnReturn -").append(initsOnReturn.toString()).append(']'); //$NON-NLS-1$
                return buffer.toString();
        }
 
@@ -121,16 +126,20 @@ public class ExceptionHandlingFlowContext extends FlowContext {
                
                int index;
                if ((index = indexes.get(exceptionType)) < 0) {
-                       return FlowInfo.DeadEnd;
+                       return FlowInfo.DEAD_END;
                }
                return initsOnExceptions[index];
        }
 
+       public UnconditionalFlowInfo initsOnReturn(){
+               return this.initsOnReturn;
+       }
+       
        public void recordHandlingException(
                ReferenceBinding exceptionType,
                UnconditionalFlowInfo flowInfo,
                TypeBinding raisedException,
-               AstNode invocationSite,
+               ASTNode invocationSite,
                boolean wasAlreadyDefinitelyCaught) {
                        
                int index = indexes.get(exceptionType);
@@ -141,16 +150,21 @@ public class ExceptionHandlingFlowContext extends FlowContext {
                        this.isNeeded[cacheIndex] |= bitMask;
                }
                this.isReached[cacheIndex] |= bitMask;
+               
                initsOnExceptions[index] =
-                       initsOnExceptions[index] == FlowInfo.DeadEnd
+                       initsOnExceptions[index] == FlowInfo.DEAD_END
                                ? flowInfo.copy().unconditionalInits()
                                : initsOnExceptions[index].mergedWith(flowInfo);
        }
        
-       public void recordReturnFrom(UnconditionalFlowInfo flowInfo) {
-               
-               // record initializations which were performed at the return point
-               initsOnReturn = initsOnReturn.mergedWith(flowInfo);
+       public void recordReturnFrom(FlowInfo flowInfo) {
+
+               if (!flowInfo.isReachable()) return; 
+               if (initsOnReturn == FlowInfo.DEAD_END) {
+                       initsOnReturn = flowInfo.copy().unconditionalInits();
+               } else {
+                       initsOnReturn = initsOnReturn.mergedWith(flowInfo.unconditionalInits());
+               }
        }
        
        /*
@@ -184,4 +198,4 @@ public class ExceptionHandlingFlowContext extends FlowContext {
                        this.extendedExceptions.add(newException);
                }
        }
-}
\ No newline at end of file
+}