Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ArrayAllocationExpression.java
index e313d57..9adf04f 100644 (file)
@@ -1,20 +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.impl.*;
-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.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.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
+
 
 public class ArrayAllocationExpression extends Expression {
 
@@ -25,8 +28,6 @@ public class ArrayAllocationExpression extends Expression {
        public Expression[] dimensions;
        public ArrayInitializer initializer;
 
-       public ArrayBinding arrayTb;
-
        /**
         * ArrayAllocationExpression constructor comment.
         */
@@ -54,45 +55,58 @@ public class ArrayAllocationExpression extends Expression {
        /**
         * Code generation for a array allocation expression
         */
-       public void generateCode(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               boolean valueRequired) {
-
-               int pc = codeStream.position;
-               ArrayBinding arrayBinding;
-
-               if (initializer != null) {
-                       initializer.generateCode(currentScope, codeStream, valueRequired);
-                       return;
-               }
-
-               int nonNullDimensionsLength = 0;
-               for (int i = 0, max = dimensions.length; i < max; i++)
-                       if (dimensions[i] != null) {
-                               dimensions[i].generateCode(currentScope, codeStream, true);
-                               nonNullDimensionsLength++;
+//     public void generateCode(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             boolean valueRequired) {
+//
+//             int pc = codeStream.position;
+//
+//             if (initializer != null) {
+//                     initializer.generateCode(currentScope, codeStream, valueRequired);
+//                     return;
+//             }
+//
+//             int nonNullDimensionsLength = 0;
+//             for (int i = 0, max = dimensions.length; i < max; i++)
+//                     if (dimensions[i] != null) {
+//                             dimensions[i].generateCode(currentScope, codeStream, true);
+//                             nonNullDimensionsLength++;
+//                     }
+//
+//             // Generate a sequence of bytecodes corresponding to an array allocation
+//             if (this.resolvedType.dimensions() == 1) {
+//                     // Mono-dimensional array
+//                     codeStream.newArray(currentScope, (ArrayBinding)this.resolvedType);
+//             } else {
+//                     // Multi-dimensional array
+//                     codeStream.multianewarray(this.resolvedType, nonNullDimensionsLength);
+//             }
+//
+//             if (valueRequired) {
+//                     codeStream.generateImplicitConversion(implicitConversion);
+//             } else {
+//                     codeStream.pop();
+//             }
+//
+//             codeStream.recordPositionsFrom(pc, this.sourceStart);
+//     }
+       public StringBuffer printExpression(int indent, StringBuffer output) {
+
+               output.append("new "); //$NON-NLS-1$
+               type.print(0, output); 
+               for (int i = 0; i < dimensions.length; i++) {
+                       if (dimensions[i] == null)
+                               output.append("[]"); //$NON-NLS-1$
+                       else {
+                               output.append('[');
+                               dimensions[i].printExpression(0, output);
+                               output.append(']');
                        }
-
-               // Generate a sequence of bytecodes corresponding to an array allocation
-               if ((arrayTb.isArrayType())
-                       && ((arrayBinding = (ArrayBinding) arrayTb).dimensions == 1)) {
-                       // Mono-dimensional array
-                       codeStream.newArray(currentScope, arrayBinding);
-               } else {
-                       // Multi-dimensional array
-                       codeStream.multianewarray(arrayTb, nonNullDimensionsLength);
-               }
-
-               if (valueRequired) {
-                       codeStream.generateImplicitConversion(implicitConversion);
-               } else {
-                       codeStream.pop();
-               }
-
-               codeStream.recordPositionsFrom(pc, this.sourceStart);
+               } 
+               if (initializer != null) initializer.printExpression(0, output);
+               return output;
        }
-
        public TypeBinding resolveType(BlockScope scope) {
 
                // Build an array type reference using the current dimensions
@@ -100,58 +114,60 @@ public class ArrayAllocationExpression extends Expression {
                // only at the -end- like new int [4][][]. The parser allows new int[][4][]
                // so this must be checked here......(this comes from a reduction to LL1 grammar)
 
-               TypeBinding referenceTb = type.resolveType(scope);
+               TypeBinding referenceType = type.resolveType(scope);
+               
                // will check for null after dimensions are checked
                constant = Constant.NotAConstant;
-               if (referenceTb == VoidBinding) {
+               if (referenceType == VoidBinding) {
                        scope.problemReporter().cannotAllocateVoidArray(this);
-                       referenceTb = null; // will return below
+                       referenceType = null;
                }
 
                // check the validity of the dimension syntax (and test for all null dimensions)
-               int lengthDim = -1;
+               int explicitDimIndex = -1;
                for (int i = dimensions.length; --i >= 0;) {
                        if (dimensions[i] != null) {
-                               if (lengthDim == -1)
-                                       lengthDim = i;
-                       } else if (
-                               lengthDim != -1) {
+                               if (explicitDimIndex < 0) explicitDimIndex = i;
+                       } else if (explicitDimIndex> 0) {
                                // should not have an empty dimension before an non-empty one
                                scope.problemReporter().incorrectLocationForEmptyDimension(this, i);
-                               return null;
                        }
                }
-               if (referenceTb == null)
-                       return null;
 
-               // lengthDim == -1 says if all dimensions are nulled
+               // explicitDimIndex < 0 says if all dimensions are nulled
                // when an initializer is given, no dimension must be specified
                if (initializer == null) {
-                       if (lengthDim == -1) {
+                       if (explicitDimIndex < 0) {
                                scope.problemReporter().mustDefineDimensionsOrInitializer(this);
-                               return null;
                        }
-               } else if (lengthDim != -1) {
+               } else if (explicitDimIndex >= 0) {
                        scope.problemReporter().cannotDefineDimensionsAndInitializer(this);
-                       return null;
                }
 
                // dimensions resolution 
-               for (int i = 0; i <= lengthDim; i++) {
-                       TypeBinding dimTb = dimensions[i].resolveTypeExpecting(scope, IntBinding);
-                       if (dimTb == null)
-                               return null;
-                       dimensions[i].implicitWidening(IntBinding, dimTb);
+               for (int i = 0; i <= explicitDimIndex; i++) {
+                       if (dimensions[i] != null) {
+                               TypeBinding dimensionType = dimensions[i].resolveTypeExpecting(scope, IntBinding);
+                               if (dimensionType != null) {
+                                       dimensions[i].implicitWidening(IntBinding, dimensionType);
+                               }
+                       }
                }
 
                // building the array binding
-               arrayTb = scope.createArray(referenceTb, dimensions.length);
+               if (referenceType != null) {
+                       if (dimensions.length > 255) {
+                               scope.problemReporter().tooManyDimensions(this);
+                       }
+                       this.resolvedType = scope.createArray(referenceType, dimensions.length);
 
-               // check the initializer
-               if (initializer != null)
-                       if ((initializer.resolveTypeExpecting(scope, arrayTb)) != null)
-                               initializer.binding = arrayTb;
-               return arrayTb;
+                       // check the initializer
+                       if (initializer != null) {
+                               if ((initializer.resolveTypeExpecting(scope, this.resolvedType)) != null)
+                                       initializer.binding = (ArrayBinding)this.resolvedType;
+                       }
+               }
+               return this.resolvedType;
        }
 
        public String toStringExpression() {
@@ -168,7 +184,7 @@ public class ArrayAllocationExpression extends Expression {
                return s;
        }
 
-       public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
+       public void traverse(ASTVisitor visitor, BlockScope scope) {
 
                if (visitor.visit(this, scope)) {
                        int dimensionsLength = dimensions.length;
@@ -182,4 +198,4 @@ public class ArrayAllocationExpression extends Expression {
                }
                visitor.endVisit(this, scope);
        }
-}
\ No newline at end of file
+}