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;
39 private Position position;
45 * @param variable the name of the variable
46 * @param initialization the initialization
47 * @param operator the assign operator
48 * @param sourceStart the start point
50 public VariableDeclaration(final Object parent,
51 final AbstractVariable variable,
52 final Expression initialization,
54 final int sourceStart) {
55 super(sourceStart, initialization.sourceEnd);
56 this.initialization = initialization;
57 this.variable = variable;
58 this.operator = operator;
60 position = new Position(sourceStart, sourceEnd);
65 * @param variable a variable (in case of $$variablename)
66 * @param sourceStart the start point
68 public VariableDeclaration(final Object parent,
69 final AbstractVariable variable,
70 final int sourceStart,
71 final int sourceEnd) {
72 super(sourceStart, sourceEnd);
73 this.variable = variable;
75 position = new Position(sourceStart, sourceEnd);
78 public void setReference(final boolean reference) {
79 this.reference = reference;
84 * @param initialization the initialization
85 * @param variable a variable (in case of $$variablename)
86 * @param sourceStart the start point
88 public VariableDeclaration(final AbstractVariable variable,
89 final Expression initialization,
91 final int sourceStart) {
92 super(sourceStart, initialization.sourceEnd);
93 this.variable = variable;
94 this.initialization = initialization;
95 this.operator = operator;
100 * @param variable a variable (in case of $$variablename)
101 * @param sourceStart the start point
103 public VariableDeclaration(final AbstractVariable variable,
104 final int sourceStart) {
105 super(sourceStart, variable.sourceEnd);
106 this.variable = variable;
110 * Return the operator as String.
111 * @return the operator
113 public final String operatorToString() {
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$
138 return "<<="; //$NON-NLS-1$
139 case RSIGNEDSHIFT_EQUAL:
140 return ">>="; //$NON-NLS-1$
142 return " unknown operator ";//$NON-NLS-1$
146 * Return the variable into String.
149 public String toStringExpression() {
150 final StringBuffer buff;
152 buff = new StringBuffer("&"); //$NON-NLS-1$
154 buff = new StringBuffer();//$NON-NLS-1$
156 buff.append(variable.toStringExpression());
157 if (initialization != null) {
158 buff.append(operatorToString()); //$NON-NLS-1$
159 buff.append(initialization.toStringExpression());
161 return buff.toString();
164 public Object getParent() {
168 public String toString() {
169 return toStringExpression();
173 * Get the image of a variable.
174 * @return the image that represents a php variable
176 public ImageDescriptor getImage() {
177 return PHPUiImages.DESC_VAR;
180 public Position getPosition() {
185 * Get the name of the field as String.
186 * @return the name of the String
188 public String name() {
189 return variable.getName();
193 * Get the variables from outside (parameters, globals ...)
194 * @return the variables from outside
196 public List getOutsideVariable() {
197 return new ArrayList(1);
201 * get the modified variables.
202 * @return the variables from we change value
204 public List getModifiedVariable() {
205 final List list = variable.getUsedVariable();
206 if (initialization != null) {
207 list.addAll(initialization.getModifiedVariable());
213 * Get the variables used.
214 * @return the variables used
216 public List getUsedVariable() {
217 if (initialization != null) {
218 return initialization.getUsedVariable();
220 return new ArrayList(1);