Refactored packagename to net.sourceforge.phpdt.internal.compiler.ast
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / UnaryExpression.java
index 8b0bbd2..d21dc2e 100644 (file)
@@ -1,20 +1,22 @@
 /*******************************************************************************
- * 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.BlockScope;
+import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
+
 
 public class UnaryExpression extends OperatorExpression {
        
@@ -30,181 +32,185 @@ public class UnaryExpression extends OperatorExpression {
                BlockScope currentScope,
                FlowContext flowContext,
                FlowInfo flowInfo) {
+                       
                if (((bits & OperatorMASK) >> OperatorSHIFT) == NOT) {
-                       return expression
+                       return this.expression
                                .analyseCode(currentScope, flowContext, flowInfo)
                                .asNegatedCondition();
                } else {
-                       return expression.analyseCode(currentScope, flowContext, flowInfo);
+                       return this.expression.analyseCode(currentScope, flowContext, flowInfo);
                }
        }
 
-       public Constant conditionalConstant() {
-               return optimizedBooleanConstant == null ? constant : optimizedBooleanConstant;
+       public Constant optimizedBooleanConstant() {
+               
+               return this.optimizedBooleanConstant == null 
+                               ? this.constant 
+                               : this.optimizedBooleanConstant;
        }
 
        /**
         * Code generation for an unary operation
         *
-        * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
-        * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
+        * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
+        * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
         * @param valueRequired boolean
         */
-       public void generateCode(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               boolean valueRequired) {
-               int pc = codeStream.position;
-               Label falseLabel, endifLabel;
-               if (constant != Constant.NotAConstant) {
-                       // inlined value
-                       if (valueRequired) {
-                               codeStream.generateConstant(constant, implicitConversion);
-                       }
-                       codeStream.recordPositionsFrom(pc, this.sourceStart);
-                       return;
-               }
-               switch ((bits & OperatorMASK) >> OperatorSHIFT) {
-                       case NOT :
-                               switch (expression.implicitConversion >> 4) /* runtime type */ {
-                                       case T_boolean :
-                                               // ! <boolean>
-                                               // Generate code for the condition
-                                               expression.generateOptimizedBoolean(
-                                                       currentScope,
-                                                       codeStream,
-                                                       null,
-                                                       (falseLabel = new Label(codeStream)),
-                                                       valueRequired);
-                                               if (valueRequired) {
-                                                       codeStream.iconst_0();
-                                                       if (falseLabel.hasForwardReferences()) {
-                                                               codeStream.goto_(endifLabel = new Label(codeStream));
-                                                               codeStream.decrStackSize(1);
-                                                               falseLabel.place();
-                                                               codeStream.iconst_1();
-                                                               endifLabel.place();
-                                                       }
-                                               } else { // 6596: if (!(a && b)){} - must still place falseLabel
-                                                       falseLabel.place();
-                                               }                                               
-                                               break;
-                               }
-                               break;
-                       case TWIDDLE :
-                               switch (expression.implicitConversion >> 4 /* runtime */
-                                       ) {
-                                       case T_int :
-                                               // ~int
-                                               expression.generateCode(currentScope, codeStream, valueRequired);
-                                               if (valueRequired) {
-                                                       codeStream.iconst_m1();
-                                                       codeStream.ixor();
-                                               }
-                                               break;
-                                       case T_long :
-                                               expression.generateCode(currentScope, codeStream, valueRequired);
-                                               if (valueRequired) {
-                                                       codeStream.ldc2_w(-1L);
-                                                       codeStream.lxor();
-                                               }
-                               }
-                               break;
-                       case MINUS :
-                               // - <num>
-                               if (constant != NotAConstant) {
-                                       if (valueRequired) {
-                                               switch (expression.implicitConversion >> 4 /* runtime */
-                                                       ) {
-                                                       case T_int :
-                                                               codeStream.generateInlinedValue(constant.intValue() * -1);
-                                                               break;
-                                                       case T_float :
-                                                               codeStream.generateInlinedValue(constant.floatValue() * -1.0f);
-                                                               break;
-                                                       case T_long :
-                                                               codeStream.generateInlinedValue(constant.longValue() * -1L);
-                                                               break;
-                                                       case T_double :
-                                                               codeStream.generateInlinedValue(constant.doubleValue() * -1.0);
-                                               }
-                                       }
-                               } else {
-                                       expression.generateCode(currentScope, codeStream, valueRequired);
-                                       if (valueRequired) {
-                                               switch (expression.implicitConversion >> 4 /* runtime type */
-                                                       ) {
-                                                       case T_int :
-                                                               codeStream.ineg();
-                                                               break;
-                                                       case T_float :
-                                                               codeStream.fneg();
-                                                               break;
-                                                       case T_long :
-                                                               codeStream.lneg();
-                                                               break;
-                                                       case T_double :
-                                                               codeStream.dneg();
-                                               }
-                                       }
-                               }
-                               break;
-                       case PLUS :
-                               expression.generateCode(currentScope, codeStream, valueRequired);
-               }
-               if (valueRequired) {
-                       codeStream.generateImplicitConversion(implicitConversion);
-               }
-               codeStream.recordPositionsFrom(pc, this.sourceStart);
-       }
-
-       /**
-        * Boolean operator code generation
-        *      Optimized operations are: &&, ||, <, <=, >, >=, &, |, ^
-        */
-       public void generateOptimizedBoolean(
-               BlockScope currentScope,
-               CodeStream codeStream,
-               Label trueLabel,
-               Label falseLabel,
-               boolean valueRequired) {
-
-               if ((constant != Constant.NotAConstant) && (constant.typeID() == T_boolean)) {
-                       super.generateOptimizedBoolean(
-                               currentScope,
-                               codeStream,
-                               trueLabel,
-                               falseLabel,
-                               valueRequired);
-                       return;
-               }
-               if (((bits & OperatorMASK) >> OperatorSHIFT) == NOT) {
-                       expression.generateOptimizedBoolean(
-                               currentScope,
-                               codeStream,
-                               falseLabel,
-                               trueLabel,
-                               valueRequired);
-               } else {
-                       super.generateOptimizedBoolean(
-                               currentScope,
-                               codeStream,
-                               trueLabel,
-                               falseLabel,
-                               valueRequired);
-               }
-       }
+//     public void generateCode(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             boolean valueRequired) {
+//                     
+//             int pc = codeStream.position;
+//             Label falseLabel, endifLabel;
+//             if (this.constant != Constant.NotAConstant) {
+//                     // inlined value
+//                     if (valueRequired) {
+//                             codeStream.generateConstant(this.constant, this.implicitConversion);
+//                     }
+//                     codeStream.recordPositionsFrom(pc, this.sourceStart);
+//                     return;
+//             }
+//             switch ((bits & OperatorMASK) >> OperatorSHIFT) {
+//                     case NOT :
+//                             switch (this.expression.implicitConversion >> 4) /* runtime type */ {
+//                                     case T_boolean :
+//                                             // ! <boolean>
+//                                             // Generate code for the condition
+//                                             this.expression.generateOptimizedBoolean(
+//                                                     currentScope,
+//                                                     codeStream,
+//                                                     null,
+//                                                     (falseLabel = new Label(codeStream)),
+//                                                     valueRequired);
+//                                             if (valueRequired) {
+//                                                     codeStream.iconst_0();
+//                                                     if (falseLabel.hasForwardReferences()) {
+//                                                             codeStream.goto_(endifLabel = new Label(codeStream));
+//                                                             codeStream.decrStackSize(1);
+//                                                             falseLabel.place();
+//                                                             codeStream.iconst_1();
+//                                                             endifLabel.place();
+//                                                     }
+//                                             } else { // 6596: if (!(a && b)){} - must still place falseLabel
+//                                                     falseLabel.place();
+//                                             }                                               
+//                                             break;
+//                             }
+//                             break;
+//                     case TWIDDLE :
+//                             switch (this.expression.implicitConversion >> 4 /* runtime */
+//                                     ) {
+//                                     case T_int :
+//                                             // ~int
+//                                             this.expression.generateCode(currentScope, codeStream, valueRequired);
+//                                             if (valueRequired) {
+//                                                     codeStream.iconst_m1();
+//                                                     codeStream.ixor();
+//                                             }
+//                                             break;
+//                                     case T_long :
+//                                             this.expression.generateCode(currentScope, codeStream, valueRequired);
+//                                             if (valueRequired) {
+//                                                     codeStream.ldc2_w(-1L);
+//                                                     codeStream.lxor();
+//                                             }
+//                             }
+//                             break;
+//                     case MINUS :
+//                             // - <num>
+//                             if (this.constant != NotAConstant) {
+//                                     if (valueRequired) {
+//                                             switch (this.expression.implicitConversion >> 4){ /* runtime */
+//                                                     case T_int :
+//                                                             codeStream.generateInlinedValue(this.constant.intValue() * -1);
+//                                                             break;
+//                                                     case T_float :
+//                                                             codeStream.generateInlinedValue(this.constant.floatValue() * -1.0f);
+//                                                             break;
+//                                                     case T_long :
+//                                                             codeStream.generateInlinedValue(this.constant.longValue() * -1L);
+//                                                             break;
+//                                                     case T_double :
+//                                                             codeStream.generateInlinedValue(this.constant.doubleValue() * -1.0);
+//                                             }
+//                                     }
+//                             } else {
+//                                     this.expression.generateCode(currentScope, codeStream, valueRequired);
+//                                     if (valueRequired) {
+//                                             switch (expression.implicitConversion >> 4){ /* runtime type */
+//                                                     case T_int :
+//                                                             codeStream.ineg();
+//                                                             break;
+//                                                     case T_float :
+//                                                             codeStream.fneg();
+//                                                             break;
+//                                                     case T_long :
+//                                                             codeStream.lneg();
+//                                                             break;
+//                                                     case T_double :
+//                                                             codeStream.dneg();
+//                                             }
+//                                     }
+//                             }
+//                             break;
+//                     case PLUS :
+//                             this.expression.generateCode(currentScope, codeStream, valueRequired);
+//             }
+//             if (valueRequired) {
+//                     codeStream.generateImplicitConversion(this.implicitConversion);
+//             }
+//             codeStream.recordPositionsFrom(pc, this.sourceStart);
+//     }
+//
+//     /**
+//      * Boolean operator code generation
+//      *      Optimized operations are: &&, ||, <, <=, >, >=, &, |, ^
+//      */
+//     public void generateOptimizedBoolean(
+//             BlockScope currentScope,
+//             CodeStream codeStream,
+//             Label trueLabel,
+//             Label falseLabel,
+//             boolean valueRequired) {
+//
+//             if ((this.constant != Constant.NotAConstant) && (this.constant.typeID() == T_boolean)) {
+//                     super.generateOptimizedBoolean(
+//                             currentScope,
+//                             codeStream,
+//                             trueLabel,
+//                             falseLabel,
+//                             valueRequired);
+//                     return;
+//             }
+//             if (((this.bits & OperatorMASK) >> OperatorSHIFT) == NOT) {
+//                     this.expression.generateOptimizedBoolean(
+//                             currentScope,
+//                             codeStream,
+//                             falseLabel,
+//                             trueLabel,
+//                             valueRequired);
+//             } else {
+//                     super.generateOptimizedBoolean(
+//                             currentScope,
+//                             codeStream,
+//                             trueLabel,
+//                             falseLabel,
+//                             valueRequired);
+//             }
+//     }
 
        public TypeBinding resolveType(BlockScope scope) {
-               TypeBinding expressionTb = expression.resolveType(scope);
-               if (expressionTb == null) {
-                       constant = NotAConstant;
+               
+               TypeBinding expressionType = this.expression.resolveType(scope);
+               if (expressionType == null) {
+                       this.constant = NotAConstant;
                        return null;
                }
-               int expressionId = expressionTb.id;
+               int expressionId = expressionType.id;
                if (expressionId > 15) {
-                       constant = NotAConstant;
-                       scope.problemReporter().invalidOperator(this, expressionTb);
+                       this.constant = NotAConstant;
+                       scope.problemReporter().invalidOperator(this, expressionType);
                        return null;
                }
 
@@ -225,64 +231,70 @@ public class UnaryExpression extends OperatorExpression {
                //  0000   0000       0000   0000      0000
                //  <<16   <<12       <<8    <<4       <<0
                int result = ResolveTypeTables[tableId][(expressionId << 4) + expressionId];
-               expression.implicitConversion = result >>> 12;
-               bits |= result & 0xF;
+               this.expression.implicitConversion = result >>> 12;
+               this.bits |= result & 0xF;
                switch (result & 0xF) { // only switch on possible result type.....
                        case T_boolean :
-                               this.typeBinding = BooleanBinding;
+                               this.resolvedType = BooleanBinding;
                                break;
                        case T_byte :
-                               this.typeBinding = ByteBinding;
+                               this.resolvedType = ByteBinding;
                                break;
                        case T_char :
-                               this.typeBinding = CharBinding;
+                               this.resolvedType = CharBinding;
                                break;
                        case T_double :
-                               this.typeBinding = DoubleBinding;
+                               this.resolvedType = DoubleBinding;
                                break;
                        case T_float :
-                               this.typeBinding = FloatBinding;
+                               this.resolvedType = FloatBinding;
                                break;
                        case T_int :
-                               this.typeBinding = IntBinding;
+                               this.resolvedType = IntBinding;
                                break;
                        case T_long :
-                               this.typeBinding = LongBinding;
+                               this.resolvedType = LongBinding;
                                break;
                        default : //error........
-                               constant = Constant.NotAConstant;
+                               this.constant = Constant.NotAConstant;
                                if (expressionId != T_undefined)
-                                       scope.problemReporter().invalidOperator(this, expressionTb);
+                                       scope.problemReporter().invalidOperator(this, expressionType);
                                return null;
                }
                // compute the constant when valid
-               if (expression.constant != Constant.NotAConstant) {
-                       constant =
+               if (this.expression.constant != Constant.NotAConstant) {
+                       this.constant =
                                Constant.computeConstantOperation(
-                                       expression.constant,
+                                       this.expression.constant,
                                        expressionId,
                                        (bits & OperatorMASK) >> OperatorSHIFT);
                } else {
-                       constant = Constant.NotAConstant;
+                       this.constant = Constant.NotAConstant;
                        if (((bits & OperatorMASK) >> OperatorSHIFT) == NOT) {
-                               Constant cst = expression.conditionalConstant();
-                               if (cst.typeID() == T_boolean)
-                                       optimizedBooleanConstant = Constant.fromValue(!cst.booleanValue());
+                               Constant cst = expression.optimizedBooleanConstant();
+                               if (cst != Constant.NotAConstant) 
+                                       this.optimizedBooleanConstant = Constant.fromValue(!cst.booleanValue());
                        }
                }
-               return this.typeBinding;
+               return this.resolvedType;
        }
-
+       public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
+               
+               output.append(operatorToString()).append(' ');
+               return this.expression.printExpression(0, output);
+       } 
        public String toStringExpressionNoParenthesis() {
-               return operatorToString() + " " + expression.toStringExpression(); //$NON-NLS-1$
+               
+               return operatorToString() + " " + this.expression.toStringExpression(); //$NON-NLS-1$
        } 
        
        public void traverse(
-               IAbstractSyntaxTreeVisitor visitor,
+           ASTVisitor visitor,
                BlockScope blockScope) {
+                       
                if (visitor.visit(this, blockScope)) {
-                       expression.traverse(visitor, blockScope);
+                       this.expression.traverse(visitor, blockScope);
                }
                visitor.endVisit(this, blockScope);
        }
-}
\ No newline at end of file
+}