Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / LocalDeclaration.java
index 51dcdab..a094fc4 100644 (file)
@@ -1,26 +1,25 @@
 /*******************************************************************************
- * 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.CodeStream;
+import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
-import net.sourceforge.phpdt.internal.compiler.impl.Constant;
 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
 import net.sourceforge.phpdt.internal.compiler.lookup.LocalVariableBinding;
 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
 
+
 public class LocalDeclaration extends AbstractVariableDeclaration {
 
        public LocalVariableBinding binding;
@@ -37,10 +36,10 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                this.sourceEnd = sourceEnd;
                if (initialization != null) {
                        this.declarationSourceEnd = initialization.sourceEnd;
+                       this.declarationEnd = initialization.sourceEnd;
                } else {
-                       this.declarationSourceEnd = sourceEnd;
+                       this.declarationEnd = sourceEnd;
                }
-               this.declarationEnd = this.declarationSourceEnd;
        }
 
        public FlowInfo analyseCode(
@@ -49,84 +48,99 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                FlowInfo flowInfo) {
 
                // record variable initialization if any
-               if (!flowInfo.isDeadEnd() && !flowInfo.isFakeReachable()) {
+               if (flowInfo.isReachable()) {
                        bits |= IsLocalDeclarationReachableMASK; // only set if actually reached
                }
-               if (initialization == null)
+               if (initialization == null) 
                        return flowInfo;
+                       
                flowInfo =
                        initialization
                                .analyseCode(currentScope, flowContext, flowInfo)
                                .unconditionalInits();
+
+               // final int i = (i = 0);
+               // no need to complain since (i = 0) part will get the blame
+               //if (binding.isFinal() && flowInfo.isPotentiallyAssigned(binding)) {
+               //      currentScope.problemReporter().duplicateInitializationOfFinalLocal(binding, this);
+               //}
+                               
                flowInfo.markAsDefinitelyAssigned(binding);
                return flowInfo;
        }
 
        public void checkModifiers() {
-               //only potential valid modifier is <<final>>
 
-               if (((modifiers & AccJustFlag) | AccFinal) != AccFinal)
+               //only potential valid modifier is <<final>>
+               if (((modifiers & AccJustFlag) & ~AccFinal) != 0)
                        //AccModifierProblem -> other (non-visibility problem)
                        //AccAlternateModifierProblem -> duplicate modifier
                        //AccModifierProblem | AccAlternateModifierProblem -> visibility problem"
-                       // -x-1 returns the bitInvert 
 
-                       modifiers =
-                               (modifiers & (-AccAlternateModifierProblem - 1)) | AccModifierProblem;
+                       modifiers = (modifiers & ~AccAlternateModifierProblem) | AccModifierProblem;
        }
 
        /**
         * Code generation for a local declaration:
-        *      i.e.&nbsp;normal assignment to a local variable + unused variable handling 
+        *        normal assignment to a local variable + unused variable handling 
         */
-       public void generateCode(BlockScope currentScope, CodeStream codeStream) {
-
-               if ((bits & IsReachableMASK) == 0) {
-                       return;
-               }
-               int pc = codeStream.position;
-               Constant inlinedValue;
-               // something to initialize?
-               if (binding.resolvedPosition != -1) {
-                       codeStream.addVisibleLocalVariable(binding);
-               }
-               if (initialization != null) {
-                       // initialize to constant value?
-                       if ((inlinedValue = initialization.constant) != NotAConstant) {
-                               // forget initializing unused or final locals set to constant value (final ones are inlined)
-                               if (binding.resolvedPosition != -1) { // may need to preserve variable
-                                       int initPC = codeStream.position;
-                                       codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
-                                       codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
-                                       codeStream.store(binding, false);
-                                       binding.recordInitializationStartPC(codeStream.position);
-                                       //                              codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
-                                       //                              codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index            
-                               }
-                       } else { // initializing to non-constant value
-                               initialization.generateCode(currentScope, codeStream, true);
-                               // if binding unused generate then discard the value
-                               if (binding.resolvedPosition != -1) {
-                                       codeStream.store(binding, false);
-                                       if (binding.initializationCount == 0) {
-                                               /* Variable may have been initialized during the code initializing it
-                                                       e.g. int i = (i = 1);
-                                               */
-                                               binding.recordInitializationStartPC(codeStream.position);
-                                               //                                      codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
-                                               //                                      codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index 
-                                       }
-                               } else {
-                                       if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
-                                               codeStream.pop2();
-                                       } else {
-                                               codeStream.pop();
-                                       }
-                               }
-                       }
-               }
-               codeStream.recordPositionsFrom(pc, this.sourceStart);
-       }
+//     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
+//
+//             // even if not reachable, variable must be added to visible if allocated (28298)
+//             if (binding.resolvedPosition != -1) {
+//                     codeStream.addVisibleLocalVariable(binding);
+//             }
+//             if ((bits & IsReachableMASK) == 0) {
+//                     return;
+//             }
+//             int pc = codeStream.position;
+//             Constant inlinedValue;
+//
+//             // something to initialize?
+//             if (initialization != null) {
+//                     // initialize to constant value?
+//                     if ((inlinedValue = initialization.constant) != NotAConstant) {
+//                             // forget initializing unused or final locals set to constant value (final ones are inlined)
+//                             if (binding.resolvedPosition != -1) { // may need to preserve variable
+//                                     int initPC = codeStream.position;
+//                                     codeStream.generateConstant(inlinedValue, initialization.implicitConversion);
+//                                     codeStream.recordPositionsFrom(initPC, initialization.sourceStart);
+//                                     codeStream.store(binding, false);
+//                                     binding.recordInitializationStartPC(codeStream.position);
+//                                     //                              codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
+//                                     //                              codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index            
+//                             }
+//                     } else { // initializing to non-constant value
+//                             initialization.generateCode(currentScope, codeStream, true);
+//                             // if binding unused generate then discard the value
+//                             if (binding.resolvedPosition != -1) {
+//                                     // 26903, need extra cast to store null in array local var      
+//                                     if (binding.type.isArrayType() 
+//                                             && (initialization.resolvedType == NullBinding  // arrayLoc = null
+//                                                     || ((initialization instanceof CastExpression)  // arrayLoc = (type[])null
+//                                                             && (((CastExpression)initialization).innermostCastedExpression().resolvedType == NullBinding)))){
+//                                             codeStream.checkcast(binding.type); 
+//                                     }                                       
+//                                     codeStream.store(binding, false);
+//                                     if (binding.initializationCount == 0) {
+//                                             /* Variable may have been initialized during the code initializing it
+//                                                     e.g. int i = (i = 1);
+//                                             */
+//                                             binding.recordInitializationStartPC(codeStream.position);
+//                                             //                                      codeStream.lastInitStateIndexWhenRemovingInits = -2; // reinitialize remove index 
+//                                             //                                      codeStream.lastInitStateIndexWhenAddingInits = -2; // reinitialize add index 
+//                                     }
+//                             } else {
+//                                     if ((binding.type == LongBinding) || (binding.type == DoubleBinding)) {
+//                                             codeStream.pop2();
+//                                     } else {
+//                                             codeStream.pop();
+//                                     }
+//                             }
+//                     }
+//             }
+//             codeStream.recordPositionsFrom(pc, this.sourceStart);
+//     }
 
        public String name() {
 
@@ -156,6 +170,9 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                        // the name already exists... may carry on with the first binding...
                        scope.problemReporter().redefineLocal(this);
                } else {
+                       if ((modifiers & AccFinal)!= 0 && this.initialization == null) {
+                               modifiers |= AccBlankFinal;
+                       }
                        binding = new LocalVariableBinding(this, tb, modifiers, false);
                        scope.addLocalVariable(binding);
                        binding.constant = NotAConstant;
@@ -182,7 +199,7 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                                if (initTb != null) {
                                        if (initialization.isConstantValueOfTypeAssignableToType(initTb, tb)
                                                || (tb.isBaseType() && BaseTypeBinding.isWidening(tb.id, initTb.id))
-                                               || BlockScope.areTypesCompatible(initTb, tb))
+                                               || initTb.isCompatibleWith(tb))
                                                initialization.implicitWidening(tb, initTb);
                                        else
                                                scope.problemReporter().typeMismatchError(initTb, tb, this);
@@ -199,7 +216,7 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                }
        }
 
-       public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
+       public void traverse(ASTVisitor visitor, BlockScope scope) {
 
                if (visitor.visit(this, scope)) {
                        type.traverse(visitor, scope);
@@ -208,4 +225,4 @@ public class LocalDeclaration extends AbstractVariableDeclaration {
                }
                visitor.endVisit(this, scope);
        }
-}
\ No newline at end of file
+}