1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
4 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
5 import org.eclipse.jface.resource.ImageDescriptor;
6 import org.eclipse.jface.text.Position;
9 import java.util.ArrayList;
12 * A variable declaration.
13 * @author Matthieu Casanova
15 public class VariableDeclaration extends Expression implements Outlineable {
17 public static final int EQUAL = 0;
18 public static final int PLUS_EQUAL = 1;
19 public static final int MINUS_EQUAL = 2;
20 public static final int STAR_EQUAL = 3;
21 public static final int SLASH_EQUAL = 4;
22 public static final int AND_EQUAL = 5;
23 public static final int OR_EQUAL = 6;
24 public static final int XOR_EQUAL = 7;
25 public static final int DOT_EQUAL = 8;
26 public static final int REM_EQUAL = 9;
27 public static final int TILDE_EQUAL = 10;
28 public static final int LSHIFT_EQUAL = 11;
29 public static final int RSIGNEDSHIFT_EQUAL = 12;
31 protected AbstractVariable variable;
33 /** The value for variable initialization. */
34 public Expression initialization;
36 private Object parent;
37 private boolean reference;
38 private Position position;
44 * @param variable the name of the variable
45 * @param initialization the initialization
46 * @param operator the assign operator
47 * @param sourceStart the start point
49 public VariableDeclaration(final Object parent,
50 final AbstractVariable variable,
51 final Expression initialization,
53 final int sourceStart) {
54 super(sourceStart, initialization.sourceEnd);
55 this.initialization = initialization;
56 this.variable = variable;
57 this.operator = operator;
59 position = new Position(sourceStart, sourceEnd);
64 * @param variable a variable (in case of $$variablename)
65 * @param sourceStart the start point
67 public VariableDeclaration(final Object parent,
68 final AbstractVariable variable,
69 final int sourceStart,
70 final int sourceEnd) {
71 super(sourceStart, sourceEnd);
72 this.variable = variable;
76 public void setReference(final boolean reference) {
77 this.reference = reference;
82 * @param initialization the initialization
83 * @param variable a variable (in case of $$variablename)
84 * @param sourceStart the start point
86 public VariableDeclaration(final AbstractVariable variable,
87 final Expression initialization,
89 final int sourceStart) {
90 super(sourceStart, initialization.sourceEnd);
91 this.variable = variable;
92 this.initialization = initialization;
93 this.operator = operator;
98 * @param variable a variable (in case of $$variablename)
99 * @param sourceStart the start point
101 public VariableDeclaration(final AbstractVariable variable,
102 final int sourceStart) {
103 super(sourceStart, variable.sourceEnd);
104 this.variable = variable;
108 * Return the operator as String.
109 * @return the operator
111 public final String operatorToString() {
114 return "="; //$NON-NLS-1$
116 return "+="; //$NON-NLS-1$
118 return "-="; //$NON-NLS-1$
120 return "*="; //$NON-NLS-1$
122 return "/="; //$NON-NLS-1$
124 return "<="; //$NON-NLS-1$
126 return "|=";//$NON-NLS-1$
128 return "^=";//$NON-NLS-1$
130 return ".="; //$NON-NLS-1$
132 return "%="; //$NON-NLS-1$
134 return "~="; //$NON-NLS-1$
136 return "<<="; //$NON-NLS-1$
137 case RSIGNEDSHIFT_EQUAL:
138 return ">>="; //$NON-NLS-1$
140 return " unknown operator ";//$NON-NLS-1$
144 * Return the variable into String.
147 public String toStringExpression() {
148 final StringBuffer buff;
150 buff = new StringBuffer("&"); //$NON-NLS-1$
152 buff = new StringBuffer();//$NON-NLS-1$
154 buff.append(variable.toStringExpression());
155 if (initialization != null) {
156 buff.append(operatorToString()); //$NON-NLS-1$
157 buff.append(initialization.toStringExpression());
159 return buff.toString();
162 public Object getParent() {
166 public String toString() {
167 return toStringExpression();
171 * Get the image of a variable.
172 * @return the image that represents a php variable
174 public ImageDescriptor getImage() {
175 return PHPUiImages.DESC_VAR;
178 public Position getPosition() {
183 * Get the name of the field as String.
184 * @return the name of the String
186 public String name() {
187 return variable.getName();
191 * Get the variables from outside (parameters, globals ...)
192 * @return the variables from outside
194 public List getOutsideVariable() {
195 return new ArrayList(1);
199 * get the modified variables.
200 * @return the variables from we change value
202 public List getModifiedVariable() {
203 final List list = variable.getUsedVariable();
204 if (initialization != null) {
205 list.addAll(initialization.getModifiedVariable());
211 * Get the variables used.
212 * @return the variables used
214 public List getUsedVariable() {
215 if (initialization != null) {
216 return initialization.getModifiedVariable();//yes it's getModified variable (in a variable declaration $a = $b, $a is modified, event if you have only $a and no initialization
218 return new ArrayList(1);