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 AbstractVariableDeclaration 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;
33 /** The value for variable initialization. */
34 public Expression initialization;
36 private Object parent;
37 private boolean reference;
38 private Position position;
43 * @param initialization the initialization
44 * @param name the name of the variable
45 * @param sourceStart the start point
47 public VariableDeclaration(final Object parent,
49 final Expression initialization,
51 final int sourceStart) {
52 super(name, sourceStart, initialization.sourceEnd);
53 this.initialization = initialization;
54 this.operator = operator;
56 position = new Position(sourceStart, sourceEnd);
61 * @param name the name of the variable
62 * @param sourceStart the start point
64 public VariableDeclaration(final Object parent,
66 final int sourceStart,
67 final int sourceEnd) {
68 super(name, sourceStart, sourceEnd);
72 public void setReference(final boolean reference) {
73 this.reference = reference;
78 * @param initialization the initialization
79 * @param name the name of the variable
80 * @param sourceStart the start point
82 public VariableDeclaration(final char[] name,
83 final Expression initialization,
85 final int sourceStart) {
86 super(name, sourceStart, initialization.sourceEnd);
87 this.initialization = initialization;
88 this.operator = operator;
93 * @param name the name of the variable
94 * @param sourceStart the start point
96 public VariableDeclaration(final char[] name,
97 final int sourceStart) {
98 super(name, sourceStart, sourceStart + name.length);
102 * Return the operator as String.
103 * @return the operator
105 public final String operatorToString() {
108 return "="; //$NON-NLS-1$
110 return "+="; //$NON-NLS-1$
112 return "-="; //$NON-NLS-1$
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$
131 case RSIGNEDSHIFT_EQUAL:
132 return ">>="; //$NON-NLS-1$
134 return " unknown operator ";//$NON-NLS-1$
138 * Return the variable into String.
141 public String toStringExpression() {
142 final StringBuffer buff;
144 buff = new StringBuffer("&$"); //$NON-NLS-1$
146 buff = new StringBuffer("$");//$NON-NLS-1$
149 if (initialization != null) {
150 buff.append(operatorToString()); //$NON-NLS-1$
151 buff.append(initialization.toStringExpression());
153 return buff.toString();
156 public Object getParent() {
160 public String toString() {
161 return toStringExpression();
165 * Get the image of a variable.
166 * @return the image that represents a php variable
168 public ImageDescriptor getImage() {
169 return PHPUiImages.DESC_VAR;
172 public Position getPosition() {
177 * Get the name of the field as String.
178 * @return the name of the String
180 public String name() {
181 return new String(name);
185 * Get the variables from outside (parameters, globals ...)
186 * @return the variables from outside
188 public List getOutsideVariable() {
189 return new ArrayList();
193 * get the modified variables.
194 * @return the variables from we change value
196 public List getModifiedVariable() {
197 final ArrayList list = new ArrayList();
198 list.add(new VariableUsage(new String(name), sourceStart));
199 if (initialization != null) {
200 list.addAll(initialization.getModifiedVariable());
206 * Get the variables used.
207 * @return the variables used
209 public List getUsedVariable() {
210 final ArrayList list = new ArrayList();
211 if (initialization != null) {
212 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