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;
11 * 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 final AbstractVariable variable;
33 /** The value for variable initialization. */
34 public Expression initialization;
36 private Object parent;
37 protected boolean reference;
39 private Position position;
46 * @param variable the name of the variable
47 * @param initialization the initialization
48 * @param operator the assign operator
49 * @param sourceStart the start point
51 public VariableDeclaration(final Object parent,
52 final AbstractVariable variable,
53 final Expression initialization,
55 final int sourceStart) {
56 super(sourceStart, initialization.sourceEnd);
57 this.initialization = initialization;
58 this.variable = variable;
59 this.operator = operator;
61 position = new Position(sourceStart, sourceEnd);
67 * @param variable a variable (in case of $$variablename)
68 * @param sourceStart the start point
70 public VariableDeclaration(final Object parent,
71 final AbstractVariable variable,
72 final int sourceStart,
73 final int sourceEnd) {
74 super(sourceStart, sourceEnd);
75 this.variable = variable;
77 position = new Position(sourceStart, sourceEnd);
80 public final void setReference(final boolean reference) {
81 this.reference = reference;
87 * @param initialization the initialization
88 * @param variable a variable (in case of $$variablename)
89 * @param sourceStart the start point
91 public VariableDeclaration(final AbstractVariable variable,
92 final Expression initialization,
94 final int sourceStart) {
95 super(sourceStart, initialization.sourceEnd);
96 this.variable = variable;
97 this.initialization = initialization;
98 this.operator = operator;
104 * @param variable a variable (in case of $$variablename)
105 * @param sourceStart the start point
107 public VariableDeclaration(final AbstractVariable variable,
108 final int sourceStart) {
109 super(sourceStart, variable.sourceEnd);
110 this.variable = variable;
114 * Return the operator as String.
116 * @return the operator
118 private String operatorToString() {
121 return "="; //$NON-NLS-1$
123 return "+="; //$NON-NLS-1$
125 return "-="; //$NON-NLS-1$
127 return "*="; //$NON-NLS-1$
129 return "/="; //$NON-NLS-1$
131 return "<="; //$NON-NLS-1$
133 return "|=";//$NON-NLS-1$
135 return "^=";//$NON-NLS-1$
137 return ".="; //$NON-NLS-1$
139 return "%="; //$NON-NLS-1$
141 return "~="; //$NON-NLS-1$
143 return "<<="; //$NON-NLS-1$
144 case RSIGNEDSHIFT_EQUAL:
145 return ">>="; //$NON-NLS-1$
147 return " unknown operator ";//$NON-NLS-1$
151 * Return the variable into String.
155 public String toStringExpression() {
156 final String variableString = variable.toStringExpression();
157 if (initialization == null) {
158 if (reference) return '&' + variableString; else return variableString;
160 final String operatorString = operatorToString();
161 final String initString = initialization.toStringExpression();
162 final StringBuffer buff = new StringBuffer(variableString.length() +
163 operatorString.length() +
164 initString.length() +
166 buff.append(variableString);
167 buff.append(operatorString); //$NON-NLS-1$
168 buff.append(initString);
169 return buff.toString();
173 public final Object getParent() {
177 public final String toString() {
178 return toStringExpression();
182 * Get the image of a variable.
184 * @return the image that represents a php variable
186 public final ImageDescriptor getImage() {
187 return PHPUiImages.DESC_VAR;
190 public final Position getPosition() {
195 * Get the name of the field as String.
197 * @return the name of the String
199 public final String name() {
200 return variable.getName();
204 * Get the variables from outside (parameters, globals ...)
206 public final void getOutsideVariable(final List list) {
210 * get the modified variables.
212 public final void getModifiedVariable(final List list) {
213 variable.getUsedVariable(list);
214 if (initialization != null) {
215 initialization.getModifiedVariable(list);
220 * Get the variables used.
222 public final void getUsedVariable(final List list) {
223 if (initialization != null) {
224 initialization.getUsedVariable(list);