1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
4 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
5 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
6 import org.eclipse.jface.resource.ImageDescriptor;
7 import org.eclipse.jface.text.Position;
10 import java.util.ArrayList;
13 * A variable declaration.
14 * @author Matthieu Casanova
16 public class VariableDeclaration extends Expression implements Outlineable {
18 public static final int EQUAL = 0;
19 public static final int PLUS_EQUAL = 1;
20 public static final int MINUS_EQUAL = 2;
21 public static final int STAR_EQUAL = 3;
22 public static final int SLASH_EQUAL = 4;
23 public static final int AND_EQUAL = 5;
24 public static final int OR_EQUAL = 6;
25 public static final int XOR_EQUAL = 7;
26 public static final int DOT_EQUAL = 8;
27 public static final int REM_EQUAL = 9;
28 public static final int TILDE_EQUAL = 10;
29 public static final int LSHIFT_EQUAL = 11;
30 public static final int RSIGNEDSHIFT_EQUAL = 12;
32 protected Variable variable;
34 /** The value for variable initialization. */
35 public Expression initialization;
37 private Object parent;
38 private boolean reference;
39 private Position position;
45 * @param initialization the initialization
46 * @param variable the name of the variable
47 * @param sourceStart the start point
49 public VariableDeclaration(final Object parent,
50 final Variable 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 name the name of the variable
65 * @param sourceStart the start point
67 public VariableDeclaration(final Object parent,
68 final Variable 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 name the name of the variable
84 * @param sourceStart the start point
86 public VariableDeclaration(final Variable 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 name the name of the variable
99 * @param sourceStart the start point
101 public VariableDeclaration(final Variable 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();
199 * get the modified variables.
200 * @return the variables from we change value
202 public List getModifiedVariable() {
203 final ArrayList list = new ArrayList();
204 list.addAll(variable.getModifiedVariable());
205 if (initialization != null) {
206 list.addAll(initialization.getModifiedVariable());
212 * Get the variables used.
213 * @return the variables used
215 public List getUsedVariable() {
216 final ArrayList list = new ArrayList();
217 if (initialization != null) {
218 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