Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / ArrayReference.java
index 9ccea6b..5d49323 100644 (file)
@@ -1,28 +1,28 @@
 /*******************************************************************************
- * 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 ArrayReference extends Reference {
        
        public Expression receiver;
        public Expression position;
 
-       public TypeBinding arrayElementBinding;
-
        public ArrayReference(Expression rec, Expression pos) {
                this.receiver = rec;
                this.position = pos;
@@ -60,121 +60,127 @@ public class ArrayReference extends Reference {
                        receiver.analyseCode(currentScope, flowContext, flowInfo));
        }
 
-       public void generateAssignment(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               Assignment assignment,
-               boolean valueRequired) {
-
-               receiver.generateCode(currentScope, codeStream, true);
-               position.generateCode(currentScope, codeStream, true);
-               assignment.expression.generateCode(currentScope, codeStream, true);
-               codeStream.arrayAtPut(arrayElementBinding.id, valueRequired);
-               if (valueRequired) {
-                       codeStream.generateImplicitConversion(assignment.implicitConversion);
-               }
-       }
+//     public void generateAssignment(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             Assignment assignment,
+//             boolean valueRequired) {
+//
+//             receiver.generateCode(currentScope, codeStream, true);
+//             position.generateCode(currentScope, codeStream, true);
+//             assignment.expression.generateCode(currentScope, codeStream, true);
+//             codeStream.arrayAtPut(this.resolvedType.id, valueRequired);
+//             if (valueRequired) {
+//                     codeStream.generateImplicitConversion(assignment.implicitConversion);
+//             }
+//     }
 
        /**
         * Code generation for a array reference
         */
-       public void generateCode(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               boolean valueRequired) {
-
-               int pc = codeStream.position;
-               receiver.generateCode(currentScope, codeStream, true);
-               position.generateCode(currentScope, codeStream, true);
-               codeStream.arrayAt(arrayElementBinding.id);
-               // Generating code for the potential runtime type checking
-               if (valueRequired) {
-                       codeStream.generateImplicitConversion(implicitConversion);
-               } else {
-                       if (arrayElementBinding == LongBinding
-                               || arrayElementBinding == DoubleBinding) {
-                               codeStream.pop2();
-                       } else {
-                               codeStream.pop();
-                       }
-               }
-               codeStream.recordPositionsFrom(pc, this.sourceStart);
-       }
-
-       public void generateCompoundAssignment(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               Expression expression,
-               int operator,
-               int assignmentImplicitConversion,
-               boolean valueRequired) {
-
-               receiver.generateCode(currentScope, codeStream, true);
-               position.generateCode(currentScope, codeStream, true);
-               codeStream.dup2();
-               codeStream.arrayAt(arrayElementBinding.id);
-               int operationTypeID;
-               if ((operationTypeID = implicitConversion >> 4) == T_String) {
-                       codeStream.generateStringAppend(currentScope, null, expression);
-               } else {
-                       // promote the array reference to the suitable operation type
-                       codeStream.generateImplicitConversion(implicitConversion);
-                       // generate the increment value (will by itself  be promoted to the operation value)
-                       if (expression == IntLiteral.One) { // prefix operation
-                               codeStream.generateConstant(expression.constant, implicitConversion);
-                       } else {
-                               expression.generateCode(currentScope, codeStream, true);
-                       }
-                       // perform the operation
-                       codeStream.sendOperator(operator, operationTypeID);
-                       // cast the value back to the array reference type
-                       codeStream.generateImplicitConversion(assignmentImplicitConversion);
-               }
-               codeStream.arrayAtPut(arrayElementBinding.id, valueRequired);
+//     public void generateCode(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             boolean valueRequired) {
+//
+//             int pc = codeStream.position;
+//             receiver.generateCode(currentScope, codeStream, true);
+//             position.generateCode(currentScope, codeStream, true);
+//             codeStream.arrayAt(this.resolvedType.id);
+//             // Generating code for the potential runtime type checking
+//             if (valueRequired) {
+//                     codeStream.generateImplicitConversion(implicitConversion);
+//             } else {
+//                     if (this.resolvedType == LongBinding
+//                             || this.resolvedType == DoubleBinding) {
+//                             codeStream.pop2();
+//                     } else {
+//                             codeStream.pop();
+//                     }
+//             }
+//             codeStream.recordPositionsFrom(pc, this.sourceStart);
+//     }
+//
+//     public void generateCompoundAssignment(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             Expression expression,
+//             int operator,
+//             int assignmentImplicitConversion,
+//             boolean valueRequired) {
+//
+//             receiver.generateCode(currentScope, codeStream, true);
+//             position.generateCode(currentScope, codeStream, true);
+//             codeStream.dup2();
+//             codeStream.arrayAt(this.resolvedType.id);
+//             int operationTypeID;
+//             if ((operationTypeID = implicitConversion >> 4) == T_String) {
+//                     codeStream.generateStringAppend(currentScope, null, expression);
+//             } else {
+//                     // promote the array reference to the suitable operation type
+//                     codeStream.generateImplicitConversion(implicitConversion);
+//                     // generate the increment value (will by itself  be promoted to the operation value)
+//                     if (expression == IntLiteral.One) { // prefix operation
+//                             codeStream.generateConstant(expression.constant, implicitConversion);
+//                     } else {
+//                             expression.generateCode(currentScope, codeStream, true);
+//                     }
+//                     // perform the operation
+//                     codeStream.sendOperator(operator, operationTypeID);
+//                     // cast the value back to the array reference type
+//                     codeStream.generateImplicitConversion(assignmentImplicitConversion);
+//             }
+//             codeStream.arrayAtPut(this.resolvedType.id, valueRequired);
+//     }
+//
+//     public void generatePostIncrement(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             CompoundAssignment postIncrement,
+//             boolean valueRequired) {
+//
+//             receiver.generateCode(currentScope, codeStream, true);
+//             position.generateCode(currentScope, codeStream, true);
+//             codeStream.dup2();
+//             codeStream.arrayAt(this.resolvedType.id);
+//             if (valueRequired) {
+//                     if ((this.resolvedType == LongBinding)
+//                             || (this.resolvedType == DoubleBinding)) {
+//                             codeStream.dup2_x2();
+//                     } else {
+//                             codeStream.dup_x2();
+//                     }
+//             }
+//             codeStream.generateConstant(
+//                     postIncrement.expression.constant,
+//                     implicitConversion);
+//             codeStream.sendOperator(postIncrement.operator, this.resolvedType.id);
+//             codeStream.generateImplicitConversion(
+//                     postIncrement.assignmentImplicitConversion);
+//             codeStream.arrayAtPut(this.resolvedType.id, false);
+//     }
+       public StringBuffer printExpression(int indent, StringBuffer output) {
+
+               receiver.printExpression(0, output).append('[');
+               return position.printExpression(0, output).append(']');
        }
+       
+       public TypeBinding resolveType(BlockScope scope) {
 
-       public void generatePostIncrement(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               CompoundAssignment postIncrement,
-               boolean valueRequired) {
-
-               receiver.generateCode(currentScope, codeStream, true);
-               position.generateCode(currentScope, codeStream, true);
-               codeStream.dup2();
-               codeStream.arrayAt(arrayElementBinding.id);
-               if (valueRequired) {
-                       if ((arrayElementBinding == LongBinding)
-                               || (arrayElementBinding == DoubleBinding)) {
-                               codeStream.dup2_x2();
+               constant = Constant.NotAConstant;
+               TypeBinding arrayType = receiver.resolveType(scope);
+               if (arrayType != null) {
+                       if (arrayType.isArrayType()) {
+                               this.resolvedType = ((ArrayBinding) arrayType).elementsType(scope);
                        } else {
-                               codeStream.dup_x2();
+                               scope.problemReporter().referenceMustBeArrayTypeAt(arrayType, this);
                        }
                }
-               codeStream.generateConstant(
-                       postIncrement.expression.constant,
-                       implicitConversion);
-               codeStream.sendOperator(postIncrement.operator, arrayElementBinding.id);
-               codeStream.generateImplicitConversion(
-                       postIncrement.assignmentImplicitConversion);
-               codeStream.arrayAtPut(arrayElementBinding.id, false);
-       }
-
-       public TypeBinding resolveType(BlockScope scope) {
-
-               constant = Constant.NotAConstant;
-               TypeBinding arrayTb = receiver.resolveType(scope);
-               if (arrayTb == null)
-                       return null;
-               if (!arrayTb.isArrayType()) {
-                       scope.problemReporter().referenceMustBeArrayTypeAt(arrayTb, this);
-                       return null;
+               TypeBinding positionType = position.resolveTypeExpecting(scope, IntBinding);
+               if (positionType != null) {
+                       position.implicitWidening(IntBinding, positionType);
                }
-               TypeBinding positionTb = position.resolveTypeExpecting(scope, IntBinding);
-               if (positionTb == null)
-                       return null;
-               position.implicitWidening(IntBinding, positionTb);
-               return arrayElementBinding = ((ArrayBinding) arrayTb).elementsType(scope);
+               return this.resolvedType;
        }
 
        public String toStringExpression() {
@@ -183,7 +189,7 @@ public class ArrayReference extends Reference {
                +position.toStringExpression() + "]"; //$NON-NLS-1$
        } 
 
-       public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
+       public void traverse(ASTVisitor visitor, BlockScope scope) {
                
                if (visitor.visit(this, scope)) {
                        receiver.traverse(visitor, scope);
@@ -191,4 +197,4 @@ public class ArrayReference extends Reference {
                }
                visitor.endVisit(this, scope);
        }
-}
\ No newline at end of file
+}