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 Variable 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 initialization the initialization
45 * @param variable the name of the variable
46 * @param sourceStart the start point
48 public VariableDeclaration(final Object parent,
49 final Variable variable,
50 final Expression initialization,
52 final int sourceStart) {
53 super(sourceStart, initialization.sourceEnd);
54 this.initialization = initialization;
55 this.variable = variable;
56 this.operator = operator;
58 position = new Position(sourceStart, sourceEnd);
63 * @param name the name of the variable
64 * @param sourceStart the start point
66 public VariableDeclaration(final Object parent,
67 final Variable variable,
68 final int sourceStart,
69 final int sourceEnd) {
70 super(sourceStart, sourceEnd);
71 this.variable = variable;
75 public void setReference(final boolean reference) {
76 this.reference = reference;
81 * @param initialization the initialization
82 * @param name the name of the variable
83 * @param sourceStart the start point
85 public VariableDeclaration(final Variable variable,
86 final Expression initialization,
88 final int sourceStart) {
89 super(sourceStart, initialization.sourceEnd);
90 this.variable = variable;
91 this.initialization = initialization;
92 this.operator = operator;
97 * @param name the name of the variable
98 * @param sourceStart the start point
100 public VariableDeclaration(final Variable variable,
101 final int sourceStart) {
102 super(sourceStart, variable.sourceEnd);
103 this.variable = variable;
107 * Return the operator as String.
108 * @return the operator
110 public final String operatorToString() {
113 return "="; //$NON-NLS-1$
115 return "+="; //$NON-NLS-1$
117 return "-="; //$NON-NLS-1$
119 return "*="; //$NON-NLS-1$
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$
136 case RSIGNEDSHIFT_EQUAL:
137 return ">>="; //$NON-NLS-1$
139 return " unknown operator ";//$NON-NLS-1$
143 * Return the variable into String.
146 public String toStringExpression() {
147 final StringBuffer buff;
149 buff = new StringBuffer("&"); //$NON-NLS-1$
151 buff = new StringBuffer();//$NON-NLS-1$
153 buff.append(variable.toStringExpression());
154 if (initialization != null) {
155 buff.append(operatorToString()); //$NON-NLS-1$
156 buff.append(initialization.toStringExpression());
158 return buff.toString();
161 public Object getParent() {
165 public String toString() {
166 return toStringExpression();
170 * Get the image of a variable.
171 * @return the image that represents a php variable
173 public ImageDescriptor getImage() {
174 return PHPUiImages.DESC_VAR;
177 public Position getPosition() {
182 * Get the name of the field as String.
183 * @return the name of the String
185 public String name() {
186 return variable.getName();
190 * Get the variables from outside (parameters, globals ...)
191 * @return the variables from outside
193 public List getOutsideVariable() {
194 return new ArrayList();
198 * get the modified variables.
199 * @return the variables from we change value
201 public List getModifiedVariable() {
202 final ArrayList list = new ArrayList();
203 list.addAll(variable.getModifiedVariable());
204 if (initialization != null) {
205 list.addAll(initialization.getModifiedVariable());
211 * Get the variables used.
212 * @return the variables used
214 public List getUsedVariable() {
215 final ArrayList list = new ArrayList();
216 if (initialization != null) {
217 list.addAll(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