The methods to get variables do not instantiate ArrayList each time, only one is...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / VariableDeclaration.java
index 5a709fd..463128a 100644 (file)
@@ -5,72 +5,141 @@ import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.Position;
 
+import java.util.List;
+import java.util.ArrayList;
+
 /**
  * A variable declaration.
  * @author Matthieu Casanova
  */
-public class VariableDeclaration extends AbstractVariableDeclaration implements Outlineable {
+public class VariableDeclaration extends Expression implements Outlineable {
+
+  public static final int EQUAL = 0;
+  public static final int PLUS_EQUAL = 1;
+  public static final int MINUS_EQUAL = 2;
+  public static final int STAR_EQUAL = 3;
+  public static final int SLASH_EQUAL = 4;
+  public static final int AND_EQUAL = 5;
+  public static final int OR_EQUAL = 6;
+  public static final int XOR_EQUAL = 7;
+  public static final int DOT_EQUAL = 8;
+  public static final int REM_EQUAL = 9;
+  public static final int TILDE_EQUAL = 10;
+  public static final int LSHIFT_EQUAL = 11;
+  public static final int RSIGNEDSHIFT_EQUAL = 12;
+
+  protected AbstractVariable variable;
 
   /** The value for variable initialization. */
   public Expression initialization;
 
   private Object parent;
   private boolean reference;
+
   private Position position;
+
+  private int operator;
+
   /**
    * Create a variable.
+   * @param variable the name of the variable
    * @param initialization the initialization
-   * @param name the name of the variable
+   * @param operator the assign operator
    * @param sourceStart the start point
    */
-  public VariableDeclaration(Object parent,
-                             char[] name,
-                             Expression initialization,
-                             int sourceStart) {
-    super(name, sourceStart, initialization.sourceEnd);
+  public VariableDeclaration(final Object parent,
+                             final AbstractVariable variable,
+                             final Expression initialization,
+                             final int operator,
+                             final int sourceStart) {
+    super(sourceStart, initialization.sourceEnd);
     this.initialization = initialization;
+    this.variable = variable;
+    this.operator = operator;
     this.parent = parent;
     position = new Position(sourceStart, sourceEnd);
   }
 
   /**
    * Create a variable.
-   * @param name the name of the variable
+   * @param variable a variable (in case of $$variablename)
    * @param sourceStart the start point
    */
-  public VariableDeclaration(Object parent,
-                             char[] name,
-                             int sourceStart,
-                             int sourceEnd) {
-    super(name, sourceStart, sourceEnd);
+  public VariableDeclaration(final Object parent,
+                             final AbstractVariable variable,
+                             final int sourceStart,
+                             final int sourceEnd) {
+    super(sourceStart, sourceEnd);
+    this.variable = variable;
     this.parent = parent;
+    position = new Position(sourceStart, sourceEnd);
   }
 
-  public void setReference(boolean reference) {
+  public void setReference(final boolean reference) {
     this.reference = reference;
   }
 
   /**
    * Create a variable.
    * @param initialization the initialization
-   * @param name the name of the variable
+   * @param variable a variable (in case of $$variablename)
    * @param sourceStart the start point
    */
-  public VariableDeclaration(char[] name,
-                             Expression initialization,
-                             int sourceStart) {
-    super(name, sourceStart, initialization.sourceEnd);
+  public VariableDeclaration(final AbstractVariable variable,
+                             final Expression initialization,
+                             final int operator,
+                             final int sourceStart) {
+    super(sourceStart, initialization.sourceEnd);
+    this.variable = variable;
     this.initialization = initialization;
+    this.operator = operator;
   }
 
   /**
    * Create a variable.
-   * @param name the name of the variable
+   * @param variable a variable (in case of $$variablename)
    * @param sourceStart the start point
    */
-  public VariableDeclaration(char[] name,
-                             int sourceStart) {
-    super(name, sourceStart, sourceStart + name.length);
+  public VariableDeclaration(final AbstractVariable variable,
+                             final int sourceStart) {
+    super(sourceStart, variable.sourceEnd);
+    this.variable = variable;
+  }
+
+  /**
+   * Return the operator as String.
+   * @return the operator
+   */
+  public final String operatorToString() {
+    switch (operator) {
+      case EQUAL:
+        return "="; //$NON-NLS-1$
+      case PLUS_EQUAL:
+        return "+=";   //$NON-NLS-1$
+      case MINUS_EQUAL:
+        return "-=";   //$NON-NLS-1$
+      case STAR_EQUAL:
+        return "*="; //$NON-NLS-1$
+      case SLASH_EQUAL:
+        return "/="; //$NON-NLS-1$
+      case AND_EQUAL:
+        return "<="; //$NON-NLS-1$
+      case OR_EQUAL:
+        return "|=";//$NON-NLS-1$
+      case XOR_EQUAL:
+        return "^=";//$NON-NLS-1$
+      case DOT_EQUAL:
+        return ".="; //$NON-NLS-1$
+      case REM_EQUAL:
+        return "%="; //$NON-NLS-1$
+      case TILDE_EQUAL:
+        return "~="; //$NON-NLS-1$
+      case LSHIFT_EQUAL:
+        return "<<="; //$NON-NLS-1$
+      case RSIGNEDSHIFT_EQUAL:
+        return ">>="; //$NON-NLS-1$
+    }
+    return " unknown operator ";//$NON-NLS-1$
   }
 
   /**
@@ -80,13 +149,13 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements
   public String toStringExpression() {
     final StringBuffer buff;
     if (reference) {
-      buff = new StringBuffer("&$"); //$NON-NLS-1$
+      buff = new StringBuffer("&"); //$NON-NLS-1$
     } else {
-      buff = new StringBuffer("$");//$NON-NLS-1$
+      buff = new StringBuffer();//$NON-NLS-1$
     }
-    buff.append(name);
+    buff.append(variable.toStringExpression());
     if (initialization != null) {
-      buff.append(" = "); //$NON-NLS-1$
+      buff.append(operatorToString()); //$NON-NLS-1$
       buff.append(initialization.toStringExpression());
     }
     return buff.toString();
@@ -105,10 +174,43 @@ public class VariableDeclaration extends AbstractVariableDeclaration implements
    * @return the image that represents a php variable
    */
   public ImageDescriptor getImage() {
-      return PHPUiImages.DESC_VAR;
+    return PHPUiImages.DESC_VAR;
   }
 
   public Position getPosition() {
     return position;
   }
+
+  /**
+   * Get the name of the field as String.
+   * @return the name of the String
+   */
+  public String name() {
+    return variable.getName();
+  }
+
+  /**
+   * Get the variables from outside (parameters, globals ...)
+   */
+  public void getOutsideVariable(final List list) {
+  }
+
+  /**
+   * get the modified variables.
+   */
+  public void getModifiedVariable(final List list) {
+    variable.getUsedVariable(list);
+    if (initialization != null) {
+      initialization.getModifiedVariable(list);
+    }
+  }
+
+  /**
+   * Get the variables used.
+   */
+  public void getUsedVariable(final List list) {
+    if (initialization != null) {
+      initialization.getUsedVariable(list);
+    }
+  }
 }