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