Methods to get used, declared and variables coming from outside in the expressions...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / VariableDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
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;
8
9 import java.util.List;
10 import java.util.ArrayList;
11
12 /**
13  * A variable declaration.
14  * @author Matthieu Casanova
15  */
16 public class VariableDeclaration extends AbstractVariableDeclaration implements Outlineable {
17
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;
31
32
33   /** The value for variable initialization. */
34   public Expression initialization;
35
36   private Object parent;
37   private boolean reference;
38   private Position position;
39
40   private int operator;
41   /**
42    * Create a variable.
43    * @param initialization the initialization
44    * @param name the name of the variable
45    * @param sourceStart the start point
46    */
47   public VariableDeclaration(final Object parent,
48                              final char[] name,
49                              final Expression initialization,
50                              final int operator,
51                              final int sourceStart) {
52     super(name, sourceStart, initialization.sourceEnd);
53     this.initialization = initialization;
54     this.operator = operator;
55     this.parent = parent;
56     position = new Position(sourceStart, sourceEnd);
57   }
58
59   /**
60    * Create a variable.
61    * @param name the name of the variable
62    * @param sourceStart the start point
63    */
64   public VariableDeclaration(final Object parent,
65                              final char[] name,
66                              final int sourceStart,
67                              final int sourceEnd) {
68     super(name, sourceStart, sourceEnd);
69     this.parent = parent;
70   }
71
72   public void setReference(final boolean reference) {
73     this.reference = reference;
74   }
75
76   /**
77    * Create a variable.
78    * @param initialization the initialization
79    * @param name the name of the variable
80    * @param sourceStart the start point
81    */
82   public VariableDeclaration(final char[] name,
83                              final Expression initialization,
84                              final int operator,
85                              final int sourceStart) {
86     super(name, sourceStart, initialization.sourceEnd);
87     this.initialization = initialization;
88     this.operator = operator;
89   }
90
91   /**
92    * Create a variable.
93    * @param name the name of the variable
94    * @param sourceStart the start point
95    */
96   public VariableDeclaration(final char[] name,
97                              final int sourceStart) {
98     super(name, sourceStart, sourceStart + name.length);
99   }
100
101     /**
102    * Return the operator as String.
103    * @return the operator
104    */
105   public final String operatorToString() {
106     switch (operator) {
107       case EQUAL:
108         return "="; //$NON-NLS-1$
109       case PLUS_EQUAL:
110         return "+=";   //$NON-NLS-1$
111       case MINUS_EQUAL:
112         return "-=";   //$NON-NLS-1$
113       case STAR_EQUAL:
114         return "*="; //$NON-NLS-1$
115       case SLASH_EQUAL:
116         return "/="; //$NON-NLS-1$
117       case AND_EQUAL:
118         return "<="; //$NON-NLS-1$
119       case OR_EQUAL:
120         return "|=";//$NON-NLS-1$
121       case XOR_EQUAL:
122         return "^=";//$NON-NLS-1$
123       case DOT_EQUAL:
124         return ".="; //$NON-NLS-1$
125       case REM_EQUAL:
126         return "%="; //$NON-NLS-1$
127       case TILDE_EQUAL:
128         return "~="; //$NON-NLS-1$
129       case LSHIFT_EQUAL:
130         return "<<="; //$NON-NLS-1$
131       case RSIGNEDSHIFT_EQUAL:
132         return ">>="; //$NON-NLS-1$
133     }
134     return " unknown operator ";//$NON-NLS-1$
135   }
136
137   /**
138    * Return the variable into String.
139    * @return a String
140    */
141   public String toStringExpression() {
142     final StringBuffer buff;
143     if (reference) {
144       buff = new StringBuffer("&$"); //$NON-NLS-1$
145     } else {
146       buff = new StringBuffer("$");//$NON-NLS-1$
147     }
148     buff.append(name);
149     if (initialization != null) {
150       buff.append(operatorToString()); //$NON-NLS-1$
151       buff.append(initialization.toStringExpression());
152     }
153     return buff.toString();
154   }
155
156   public Object getParent() {
157     return parent;
158   }
159
160   public String toString() {
161     return toStringExpression();
162   }
163
164   /**
165    * Get the image of a variable.
166    * @return the image that represents a php variable
167    */
168   public ImageDescriptor getImage() {
169     return PHPUiImages.DESC_VAR;
170   }
171
172   public Position getPosition() {
173     return position;
174   }
175
176   /**
177    * Get the name of the field as String.
178    * @return the name of the String
179    */
180   public String name() {
181     return new String(name);
182   }
183
184   /**
185    * Get the variables from outside (parameters, globals ...)
186    * @return the variables from outside
187    */
188   public List getOutsideVariable() {
189     return new ArrayList();
190   }
191
192   /**
193    * get the modified variables.
194    * @return the variables from we change value
195    */
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());
201     }
202     return list;
203   }
204
205   /**
206    * Get the variables used.
207    * @return the variables used
208    */
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
213     }
214     return list;
215   }
216 }